How to create a Hibernate mapping in IntelliJ and H2 - java

I connect h2 database and Hibernate in IntelliJ, but when I run the JPA Console I receive this:
StudentBean is not mapped [from StudentBean]
I am new in Hibernate and I will be crateful if someone shom me step by step how to mapping classes in JPA Hibernate
P.S. I awready import all necessary libraries

The problem with maping is resolved for me:
image
On the image you can see the correct path tree and my jar libraries.
But this path working for desctop app not for web.

Related

Grails not mapping entities from hibernate.cfg.xml file

I am trying to create a sample project to demonstrate the use of grails 5 with gorm 7.
Here I am trying to map hibernate entities using hibernate.cfg.xml file. The hibernate.cfg.xml file is placed under grails-app/conf/ diectory.
The hibernate entity java classes are annotated with javax.persistence.Entity annotation.
But these entity classes are not mapped by grails and performing any save, list etc operations on these entities throws org.hibernate.MappingException: Unknown entity exception.
Please find the sample project here.
Can you try moving the hibernate.cfg file to hibernate folder or refer the path in application.yml https://docs.grails.org/3.0.x/guide/hibernate.html

I can not add hibernate in intellij

I use IntelliJ to hibernate but it does add error.
I follow video https://www.youtube.com/watch?v=bGl4u44WRiI
this error:
[2016-05-16 07:44:21] javax.persistence.PersistenceException:
[PersistenceUnit: persistenceUnit] class or package not found
[2016-05-16 07:44:21] java.lang.ClassNotFoundException:
models.PmaExportTemplates
From what I noticed about intellij is if you import the hibernate from the project structure to include support. You don't get all the files required. I am not sure what you are trying to do if you are wanting to use JPA and hibernate as a provider, then I would download hibernate separately from hibernate.org and put the lib -> required folder contents and JPA folder content into its own folder along with what jdbc driver you would be using. Ex: SQL, MYSQL ect. If you are wanting to use the function in intellij you also have to provide a persistance.xml file and Java EE persistance support. I would suggest if you are just wanting to save something using hibernate that you download the files as said and then create the hibernate library when you make the project. If you want to avoid using EntityManagerFactory and use native hibernate API then you would be using hibernate.cfg.xml and a (class).hbm.xml mapping file or JPA annotations in the source code. I would suggest using SessionFactory and configure that full support of hibernate. Can you clarify what you want to do?

NoSuchMethodError: org.hibernate.SessionFactory.openSession()

I'm having hibernate3.jar and hibernate-core-4.2.0.CR1.jar in my classpath and I'm using Spring 3.1.3 version. Code got compiles sucessfully but while runtime I'm getting following error
2014-10-28 10:51:25,174 DEBUG [RMI TCP Connection(2)-10.126.30.203] -
Target method failed for RemoteInvocation: method name
'getPriceByKeys'; parameter types [java.util.List, java.util.Date]
java.lang.NoSuchMethodError:
org.hibernate.SessionFactory.openSession()Lorg/hibernate/Session;
I google it but not find any solution. Please note : My project needs hibernate3.jar but at the same time my project dependent on some other 3rd party jar which inturn using hibernet4.2.0 jar.
Any help Pls ??
You need to remove multiple hibernate JAR files from your classpath. Without doing this, your application may not work as you expect; which means you need to migrate the hibernate version of your application from 3 to 4.1.
Although Spring 3.1 uses Hibernate 3 JAR files, you can still migrate to Hibernate 4. Check out Spring blog gives a small tutorial to do so.
Migrating to Spring 3.1 and Hibernate 4.1
As part of the Core-Spring course, we have a lab application that we
use to show how to integrate Spring and JPA/Hibernate together. We
have just upgraded it to Spring 3.1 / Hibernate 4.1, and thought we
should share a few tips.
Just an update. The cause of problem is , I'm having two spring-context xml files in project (one of my project and one related to another module that I'm integrating). I'm loading context xmls from two different classes. So one of the DAO class loaded by one of spring-context xml not getting the hibernate Session.
Later on using import tag, I included 2nd spring application context file in 1st application context file and then loaded a Single application context file from the class. It solved the error.
Thanks,

Liquibase JPA configuration of the referenceUrl

I am struggling to build the JPA annotated classes diff changesets against my database, using liquibase.
Still, I am very confused about few things.
I use the following :
liquibase.properties
#liquibase.properties
driver: org.postgresql.Driver
classpath: real_path/.m2/repository/org/postgresql/postgresql/9.2-1002-jdbc4/postgresql-9.2-1002-jdbc4.jar
url: jdbc:postgresql://localhost:5432/diquiz
username: postgres
password: postgres
referenceUrl: hibernate:ejb3:diQuiz
referenceUsername: postgres
referencePassword: postgres
changeLogFile: changelog-master.xml
and
java -jar real_path\liquibase-core-3.0.6.jar diffChangeLog
and a normal persistence.xml file with standard JPA configuration.
I get an error which says : Liquibase diffChangeLog Failed: java.lang.RuntimeException: Cannot find database driver: Driver class was not specified and could not be determined from the url (hibernate:ejb3:unit)
I am confused because the below answer says that we need a hibernate.cfg.xml file (even I have persistence.xml instead), but then he says that we can use some url's which are defined on wiki page.
Hibernate using JPA (annotated Entities) and liquibase
Wiki page says that if we need to use JPA, we can choose between three types of URL's.
hibernate:ejb3:myPersistenceUnit
hibernate:ejb3:com.example.MyConfigFactory
hibernate:ejb3:myPersistenceUnit?hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
So, I choose the first one, and I've set in the liquidbase.properties this as referenceUrl.
Also, on the wiki page it is mentioned : (https://github.com/liquibase/liquibase-hibernate/wiki)
If you are using the command line version of Liquibase, you simply
have to add the liquibase-hibernate[3|4].jar file to the
LIQUIBASE_HOME/lib directory.
I did this too.
Still, doesnt work. Can someone explain me somewhow what am I missing ?
Thanks a lot!
The problem is that you are calling liquibase with "java -jar". The Class-path line in the jar can't pick up additional jars from the lib directory and so the liquibase-hibernate.jar is not being included in the classpath.
Run liquibase using the sh/bat file included in the liquibase jar: real_path/liquibase[.bat] diffChangeLog

GWT with JPA - no persistence provider

GWT with JPA
There are two projects in my eclipse workspace, let's name them:
-JPAProject
-GWTProject
JPAProject contains JPA configuration stuff (persistence.xml, entity classes and so on). GWTProject is an examplary GWT project (taken from official GWT tutorial).
Both projects work fine alone. That is, I can create EMF (EntityManagerFactory) in JPAProject and get entities from the database. GWTProject works fine too, I can run it, fill the field text in the browser and get the response.
My goal is to call JPAProject from GWTProject to get entities. But the problem is that when calling DAO, I get the following exception:
[WARN] Server class 'com.emergit.service.dao.profile.ProfileDaoService' could not be found in the web app, but was found on the system classpath
[WARN] Adding classpath entry 'file:/home/maliniak/workspace/emergit/build/classes/' to the web app classpath for this session
[WARN] /gwttest/greet
javax.persistence.PersistenceException: No Persistence provider for EntityManager named emergitPU
at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
at com.emergit.service.dao.profile.JpaProfileDaoService.<init>(JpaProfileDaoService.java:19)
at pl.maliniak.server.GreetingServiceImpl.<init>(GreetingServiceImpl.java:21)
...
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
[ERROR] 500 - POST /gwttest/greet (127.0.0.1) 3812 bytes
I guess that the warnings at the beginning can be omitted for now.
Do you have any ideas? I guess I am missing some basic point. All hints are highly appreciated.
Update: My persistence provider is well defined, the JPA project works well by itself.
I guess it's classpath related problem too. When running the GWT project, in WEB-INF/lib there is only gwt-servlet.jar.
Do you think that making custom Ant file to build whole thing up is the only solution (i.e. make jar out of the JPA project and copying it to WEB-INF/lib)? Or is there any Eclipse solution, so I could set the GWT project properties properly so GWT project would know to include persistence.xml file?
Update: OK, I got it working. I tried to put persitence.xml everywhere in war/WEB-INF where it was possible, but kept getting 'no persistence provider' error. It turned out that it wasn't about persistence.xml. I didn't copy the eclipselink jar to WEB-INF/lib, so it couldn't find provider class defined in persistence.xml. Copying all the jars did the thing.
Thank you very much Pascal.
Struggling a lot with the same error message, I solved the problem with copying all jars (eclipselink.jar, eclipselink-jpa-modelgen_2.1.0.v20100614-r7608.jar, javax.persistence_1.0.0.jar, javax.persistence_2.0.1.v201006031150.jar) from the EclipseLink zip to the .../war/WEB-INF/lib folder of my GWT project to make everything available for the Jetty in hosted mode.
As you can see from the list of files, I was using EclipseLink 2.1. Please adapt this list to your JPA implementation, if necessary.
Hoping, that this may help you as well.
Do you have a persistence provider declared in your persistence.xml? Something like this (I'm using Hibernate here, adapt it to whatever persistence provider you're using):
<persistence
<persistence-unit name="emergitPU" transaction-type="...">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
...
</persistence-unit>
</persistence>
If you do, then I suspect a classpath problem. Did you package the JPA project correctly in the webapp i.e. in WEB-INF/lib?

Categories