I'm using persistense api to connect to database
this is pom.xml dependendcies
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.8.Final</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
persistense.xml:
<?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="firstOne">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.ejb.cfgfile" value="/META-INF/hibernate.cfg.xml" />
</properties>
</persistence-unit>
</persistence>
hibernate.cfg.xml:
<?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="connection.driver_class">org.postgresql.Driver</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">tauren993</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernate</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.generate_statistics">false</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.cache.use_structured_entries">false</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<mapping class="Employee" />
</session-factory>
</hibernate-configuration>
hibernate.cfg.xml and persistense.xml are stored project/META-INF folder and I'm getting error on
EntityManagerFactory emf = Persistence.createEntityManagerFactory("firstOne");
Stack Trace:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named firstOne
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at Main.main(Main.java:14)
In your persistance-unit "firstOne" you should include the provider. say like below:
<persistence-unit name="firstOne">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
You probably miss the entities declaration. If you're in JAVA EE context ensure that you have some classes annotated as entities in your project. Otherwise, in a JAVA SE context you must define the provider as thiyaga said (see the doc) and specify the entities as below :
<persistence-unit name="OrderManagement">
[...]
<jar-file>MyOrderApp.jar</jar-file>
or / and
<class>com.widgets.Order</class>
<class>com.widgets.Customer</class>
</persistence-unit>
Moreover,
You should use JPA 2 if you can
You doesn't need anymore the hibernate.cfg.xml file, just put directly your properties in the <properties> element of the persitence.xmlfile, ensure that the hibernate properties are well prefixed with hibernate.
Related
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.
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 the following problem:
The application is deployed successfully, but whenever i try to execute a query it ends up like this:
Caused by: java.lang.IllegalArgumentException: Not an entity: class javaeetutorial.addressbook.entity.Contact
So here are my configs:
Persistence.xml
<persistence-unit name="address-bookPU" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc/PostgreSQLDataSource</jta-data-source>
<class>javaeetutorial.addressbook.entity.Contact</class>
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />
<property name="show_sql" value="true"/>
</properties>
</persistence-unit>
The entity class is annotated with #Entity of course.
Can anyone help me solve this issue?
I spent two days and found the following solution.
1. Use at least Hibernate 4.3.10:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.10.Final</version>
</dependency>
2. Change persistence.xml as follows (schema 2.1, provider
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>, two properties - a MUST):
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="your_pu" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc/your_src</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm, jar"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
</properties>
</persistence-unit>
</persistence>
I'm trying to uses queries in my Cassandra DB with Kundera (Cassandra ORM), this queries work in an others project but when I try to do it in webapp (using tomcat 6.0), I got this error :
com.impetus.kundera.metadata.KunderaMetadataManager - No Entity metadata found for the class
=> JavaNullPointerException.
But when I leave the persistence.xml from my project I got an other error. (NoPersistence.xml found or something ... )
So, my project found Persistence.xml, but not my Entity class : fileCassandra.
You can see my persistence.xml :
<?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_2_0.xsd"
version="2.0">
<!-- 192.168.3.107 -->
<persistence-unit name="cassandra_pu">
<provider>com.impetus.kundera.KunderaPersistence</provider>
<class>net.***.common.db.***.FileCassandra</class>
<properties>
<property name="kundera.nodes" value="localhost"/>
<property name="kundera.port" value="9160"/>
<property name="kundera.keyspace" value="KunderaExamples"/>
<property name="kundera.dialect" value="cassandra"/>
<property name="kundera.client.lookup.class" value="com.impetus.client.cassandra.pelops.PelopsClientFactory" />
<property name="kundera.cache.provider.class" value="com.impetus.kundera.cache.ehcache.EhCacheProvider"/>
<!-- <property name="kundera.cache.config.resource" value="/ehcache-test.xml"/> -->
</properties>
</persistence-unit>
</persistence>
net..common.db..FileCassandra I must replace by * because it's name from my companie ;)
I have embedded derby database and i work with jpa. This is my persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence
version="2.0"
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_2_0.xsd">
<persistence-unit name="pers">
<class>entities.Leverancier</class>
<class>entities.Prijsproduct</class>
<class>entities.Product</class>
</persistence-unit>
</persistence>
What should i change or add to get this working. When I run my code now I get the following:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named pers
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
at test.Test.main(Test.java:19)
Your persistence.xml is not correct. Look at a sample below:
<persistence-unit name="MyAppPU" transaction-type="RESOURCE_LOCAL">
<!-- This is where you mention your JPA runtime provider e.g. it's EclipseLink here -->
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>mypkg.MyEntity</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/my_schema"/>
<property name="javax.persistence.jdbc.password" value="pass"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.user" value="user"/>
</properties>
</persistence-unit>
You also have to make sure that you put your JPA provider jar files (along with the derby-client jar) in the classpath.