I'm writing a project using Maven and JPA (not a web app!).
I wrote my annotated entity classes and the CRUD service classes. But now, I am required to use javax.persistence.EntityManager instead of org.hibernate.Session to perform these CRUD operations.
I'm using Hibernate as JPA provider and I have everything configured on hibernate.cfg.xml under resources folder and things are running properly for now.
I know that in order to create EntityManagerFactory like
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName),
then I have to have persistence.xml under META-INF but I don't have this xml.
My question is: how do I migrate from Hibernate Sessions to JPA EntityManagers? And what's the equivalent of my hibernate config xml file in persistence config file?
Note: I cannot convert my project to JPA (there's no option).
Here's my hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
<property name="hibernate.connection.url">jdbc:derby:D:/db/derby/svn;create=true</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property> <!-- org.hibernate.context.internal.ManagedSessionContextThreadLocalSessionContext -->
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="entity.Pe" />
<mapping class="entity.Us" />
<mapping class="entity.Te" />
</session-factory>
</hibernate-configuration>
persistence.xml is very similar to hibernate.cfg.xml. Following this you can find a very straightforward comparative.
I think that "just create a folder META-INF and place under it the equivalent persistence.xml" should do the trick. Folder must be declared in a registered source folder for your project, usually is src/main/resources/META-INF.
Then you can execute this line:
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("YourPersistenceUnitNameHere")
You can simply convert it to JPA project by creating META-INF folder under src/main/java/ containing persistence.xml
The persistence.xml is very similar, here:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="persistence-unit-name">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>entity.Pe</class>
<class>entity.Us</class>
<class>entity.Te</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
<property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="hibernate.connection.url" value="jdbc:derby:D:/db/derby/svn;create=true" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.current_session_context_class"
value="org.hibernate.context.internal.ThreadLocalSessionContext" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
The reason there's no option project-RightClick < Configure < Convert to JPA is because your version of Eclipse is not Enterprise Edition. Right?
Download the latest version of Eclipse IDE: Eclipse Mars2 J2EE: http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/mars/2/eclipse-jee-mars-2-win32-x86_64.zip
Or you can Help < Install New Software; http://download.eclipse.org/releases/luna or whatever version you have.
and then select JPA and EE plugins.
Related
I found some solutions in the forum, but they not working for me.
The problem is that when I run main method I receive this exception -
PersistenceException: No Persistence provider for EntityManager
On the imane is my persistance.xml file pat but it's not working. I try to change file path to web/WEB-INF/classes/META-INF, but its's still not working.
Here my full persistance.xml source
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
<persistence-unit name="StudentPU">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.classes.StudentBean</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:h2:tcp://localhost/~/Students"/>
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
<property name="hibernate.connection.username" value="doncho"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
</properties>
</persistence-unit>
</persistence>
And one more thing? Why HibernatePersistence is stricken?
I resolve "stricken" mistake like that -
Is it correct?
But anyway the PersistenceException: No Persistence provider for EntityManager still there.
I tryed - this and this - nothings happened
I would be grateful for any ideas to resolve this error because #Entity method is important to me.
Best regards,
D. Balamjiev
The workig method for me is to create this path in my project tree:
web/WEB-INF/classes/META-INF/persistence.xml
This is working path when We create Web Applications with IntelliJ.
I am trying to learn JPA with Hibernate implementation. There are lot of blog of net about this but still i am struggling to implement it. I have written one Dao class which creates the EntityManagerFactory. But while looking this PERSISTENCE_UNIT it says
javax.persistence.PersistenceException: No Persistence provider for EntityManager named test
private final String PERSISTENCE_UNIT = "test";
private EntityManager entityManager;
public GenericDao() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory(PERSISTENCE_UNIT);
entityManager = factory.createEntityManager();
}
I also created one persistence.xml file which is in META-INF folder of web application. It looks like this.
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgres://localhost:1532/test" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="postgres" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
For deployment Tomcat8 is being used. Please let me know why application not able to lookup PERSISTENCE_UNIT.
As per comment - the problem is your persistence.xml location. It needs to be exactly where your application expects it to be:
WEB-INF/classes/META-INF/persistence.xml
Now, why did you encounter this problem is another matter, connected with how you create your .war file. Solution that should work however, is trusting Maven to take care of that. If you set packaging to war, and put your META-INF folder into resources (src/main/resources/META-INF/persistence.xml) it should take care of things.
I have a MySQL Server running on an Apache Tomcat.
I also have created a Tapestry project via Quickstart Archetype. I have then added dependencies to tapestry-hibernate and and the mysql connector in the POM.
<dependency>
<groupId>org.apache.tapestry</groupId>
<artifactId>tapestry-hibernate</artifactId>
<version>${tapestry-release-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
I have created an entity with the appropriate annotations (Entity, Id, Table, Column, etc.).
I do not want to use the Hibernate API, but the Hibernate JPA "subset".
I have a persistence.xml in the java/main/resources/META-INF folder and a hibernate.cfg.xml in the java/main/resources folder.
The persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="2.0">
<persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.show_sql" value = "true" />
<property name="hibernate.format_sql" value = "true" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/someDB" />
<property name="javax.persistence.jdbc.user" value="someUser" />
<property name="javax.persistence.jdbc.password" value="somePassword" />
</properties>
</persistence-unit>
</persistence>
My hibernate.cfg.xml:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- For use of Hibernate with a MySQL-DB over a webserver -->
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/someDB"</property>
<property name="hibernate.connection.username">someUser</property>
<property name="hibernate.connection.password">somePassword</property>
<!-- List of XML mapping files -->
</session-factory>
</hibernate-configuration>
I started the tapestry application using jetty:run.
The problem: The app starts, I can view the index page, but the database isn't accessed or modified.
I want the database and tables to be created automatically. I know that hibernate.hbm2ddl.auto=create creates the tables. Does it also create the database or does it have to be created manually?
I only have one entity and the basic tapestry pages in my code. Do I explicitly have to at least define an EntityManager to make the program create the tables?
Do I have to have both the persistence.xml and the hibernate.cfg.xml? It seems some properties are redundant...
If I want to use Hibernate just as a JPA implementation and not the hibernate-specific API, are the properties in the configuration files the correct ones?
First of all add the dependencies in the pom for the mysql -
<dependency>
<groupId>org.apache.tapestry</groupId>
<artifactId>tapestry-hibernate</artifactId>
<version>${tapestry-release-version}</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hibernate-tapestry-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-version}</version>
</dependency>
and modify your hibernate.cfg.xml file with the following configuration
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tapestry_db?createDatabaseIfNotExist=true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.format_sql">true</property>
</session-factory>
</hibernate-configuration>
This is working for me.
I have JSF Application with Hibernate. I use Tomcat connection pool in hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
<!-- using Tomcat connections -->
<property name="connection.datasource">java:/comp/env/jdbc/netstat</property>
<!-- Parameters for Hibernate connection provider
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/netstat</property>
<property name="connection.username">netstat</property>
<property name="connection.password">netstat</property>
-->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- configuration pool via c3p0 -->
<!--
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">100</property>
<property name="c3p0.max_statements">200</property>
<property name="c3p0.timeout">600</property>
-->
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping classes -->
<mapping class="ru.gooamoko.model.Group" />
<mapping class="ru.gooamoko.model.Host" />
<mapping class="ru.gooamoko.model.Traffic" />
<mapping class="ru.gooamoko.model.DailyTraffic" />
</session-factory>
</hibernate-configuration>
Project can be build with maven with all tests skipped and runs normal. But for tests souch Hibernate configuration is not applicable.
Is any way for this case to specify different configuration file for JUnit tests?
Best regards.
put the test file in src/test/resources and that will take precedence in classpath while executing tests
i have existing project with datasource setup with spring.xml .
the xml look like below
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="url"
value="jdbc:oracle:thin:#xxx:1511:xxx"/>
<property name="driverClassName"
value="oracle.jdbc.OracleDriver"/>
<property name="username" value="xxx"/>
<property name="password" value="xxx"/>
</bean>
inside eclipse, i click new ->other->hibernate-> hibernate console configuration. how to use hibernate tool inside eclipse to connect to database using setting of spring.xml file like above? or is there way to manually enter those setting in hibernate console inside eclipse to connect to database so that i can do reverse engineering on database table?
Install hibernate tools first:
you can look at Hibernate Tools setup for screen shots
here is a sample file which you can use as it as just entering your DB credentials
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">username</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#172.21.91.41:1521:DB</property>
<property name="hibernate.connection.username">passWord</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
</session-factory>
</hibernate-configuration>
create hibernate.cfg.xml file with the above text and import it while configuring hibernate tools
you can enter these configurations in hibernate.cfg.xml using the property tags
refer here for a list of values you can configure..