I know this question has been out there for a while but I tried about 10 different solutions and nothing worked. I'm new to Hibernate and Maven, so I'm a bit lost.
I've checked if persistence is inside Meta-inf,
added provider tag under persistence unit,
checked libraries
and so on...
import com.bookstore.entity.Users;
public class UsersTest {
public static void main(String[] args) {
Users user1 = new Users();
user1.setEmail("pablo.the.souza#gmail.com");
user1.setFullName("Pablo Souza");
user1.setPassword("123456");
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("BookStoreWebsite");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(user1);
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
System.out.println("A users object was persisted");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.1">
<persistence-unit name="BookStoreWebsite">
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/bookstore" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
</properties>
</persistence-unit>
</persistence>
I changed the Hibernate core dependency from:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.0.0.Alpha5</version>
<scope>compile</scope>
<type>pom</type>
</dependency>
to this:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
And it works. Not sure why unfortunately.
Related
I am new in jpa. I am using eclipselink for that. So i am trying to a simple maven project. But it doesnt work. I am getting this error (full stack trace):
Internal Exception: java.lang.NullPointerException: Cannot invoke "org.eclipse.persistence.internal.sessions.AbstractSession.getName()" because "this.session" is null
at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:127)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:115)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:188)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at main.Main.main(Main.java:16)
Caused by: java.lang.NullPointerException: Cannot invoke "org.eclipse.persistence.internal.sessions.AbstractSession.getName()" because "this.session" is null
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:2027)
at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.callPredeploy(JPAInitializer.java:100)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:104)
My main class:
package main;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import dao.PersonDao;
import model.JpaPerson;
public class Main {
public static void main(String[] args) {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("JpaPersonUnit");// block 16. Exception coming from there
EntityManager manager = factory.createEntityManager();
EntityTransaction transaction=manager.getTransaction();
PersonDao pd=new PersonDao(manager);
transaction.begin();
JpaPerson person1=new JpaPerson(0, "Efe", "Yanıkkollu", "Büyük Efe İmparatorluğu");
JpaPerson person2=new JpaPerson(1, "Yavuz Selim", "Yanıkkollu", "Türkiye");
pd.create(person3);
pd.create(person2);
pd.create(person1);
transaction.commit();
System.out.println(pd.getPersonById(2).getName());
transaction.begin();
pd.delete(person3);
transaction.commit();
int count=0;
for(JpaPerson p : pd.read()) {
System.out.println("Name: " + p.getName());
count ++;
}
System.out.println(count);
}
}
my persistence.xml (in META-INF):
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2" 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_2.xsd">
<persistence-unit name="JpaPersonUnit">
<class>model.JpaPerson</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpa.schema?serverTimezone=UTC" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="12345" />
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.logging.level" value="ALL"/>
</properties>
</persistence-unit>
</persistence>
my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>EclipseLink01</groupId>
<artifactId>EclipseLink01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<eclipselink.version>2.6.4</eclipselink.version>
<mysql.version>6.0.4</mysql.version>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>${eclipselink.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</project>
there is nothing wrong with the structure of my entity class. And jpa.schema is exist on mysql.
I have also received this error before when I wanted to run another jpa project. What should I do to fix this?
Your persistence.xml is missing a few points, specifically the transaction type (resource local if outside a container) and the provider. See the oracle docs for a good example, but you might try:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2" 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_2.xsd">
<persistence-unit name="JpaPersonUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpa.schema?serverTimezone=UTC" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="12345" />
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.logging.level" value="ALL"/>
</properties>
</persistence-unit>
</persistence>
hi all i got two different error when i run my java apps using hibernate persistence.xml
1. Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named FastMatch
2.Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver
for the first error i have add my persistence.xml like this one :
here my persistence.xml
<?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="FastMatch" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.dxtr.hibernate.newOrderSingleEntity</class>
<properties>
<property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://url/tables" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="eclipselink.logging.level.connection" value="FINEST"/>
</properties>
</persistence-unit>
</persistence>
for the second i have add in my depedency
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
and
still got error like this one
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named FastMatch
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at com.dxtr.Transaction.FastMatchTransaction.FastmatchTransaction.main(FastmatchTransaction.java:12)
and here my pom.xml
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.0</version>
</dependency>
i have try add javax.persistence.2.2.jar and other solution from other link here but this problem cannot run well
for more information i use mysql5.7 and eclipse for IDE. and for running this code i use aws. i have try all of the code in stackoverflow but still got the problem so how to fix it ?
fuad
I created the lagom project and connect to MySQL db with jpa persistence. My configurations files are below. when i run this code table was not created in db, and it displays the error is follow. Please any one help me.
This error diplays after some secons i dont know what i missing in this code.
Error
akka.pattern.AskTimeoutException: Ask timed out on [Actor[akka://rdapupdate-impl-application/user/jd
bcCreateTables-singleton/singleton/jdbcCreateTables#-898354221]] after [20000 ms]. Message of type [com.lightbend.lagom.internal.persistence.cluster.ClusterStartupTaskActor$Execute$] was sent by [Actor[akka://rdapupdate-impl-application/user/jdbcCreateTables-singleton/singleton/jdbcCreateTables#-898354221]]. A typical reason for `AskTimeoutException` is that the recipient actor didn't send a reply.
at akka.pattern.PromiseActorRef$.$anonfun$defaultOnTimeout$1(AskSupport.scala:635)
at akka.pattern.PromiseActorRef$.$anonfun$apply$1(AskSupport.scala:650)
at akka.actor.Scheduler$$anon$4.run(Scheduler.scala:205)
at scala.concurrent.Future$InternalCallbackExecutor$.unbatchedExecute(Future.scala:870)
at scala.concurrent.BatchingExecutor.execute(BatchingExecutor.scala:109)
at scala.concurrent.BatchingExecutor.execute$(BatchingExecutor.scala:103)
at scala.concurrent.Future$InternalCallbackExecutor$.execute(Future.scala:868)
at akka.actor.LightArrayRevolverScheduler$TaskHolder.executeTask(LightArrayRevolverScheduler.scala:328)
at akka.actor.LightArrayRevolverScheduler$$anon$4.executeBucket$1(LightArrayRevolverScheduler.scala:279)
at akka.actor.LightArrayRevolverScheduler$$anon$4.nextTick(LightArrayRevolverScheduler.scala:283)
at akka.actor.LightArrayRevolverScheduler$$anon$4.run(LightArrayRevolverScheduler.scala:235)
at java.lang.Thread.run(Thread.java:748)
application.conf
#
# Copyright (C) 2016 Lightbend Inc. <http://www.lightbend.com>
#
play.modules.enabled += sample.rdapupdate.impl.RdapUpdateServiceModule
rdapupdate.cassandra.keyspace = stream
cassandra-journal.keyspace = ${rdapupdate.cassandra.keyspace}
cassandra-snapshot-store.keyspace = ${rdapupdate.cassandra.keyspace}
lagom.persistence.read-side.cassandra.keyspace =
${rdapupdate.cassandra.keyspace}
akka.cluster.sharding.state-store-mode = ddata
akka.actor.serialization-bindings {
"akka.Done" = akka-misc
"akka.actor.Address" = akka-misc
"akka.remote.UniqueAddress" = akka-misc
}
db.default {
driver = "com.mysql.cj.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/test"
}
lagom.persistence.jdbc.create-tables.auto = true
jdbc-defaults.slick.driver = "slick.driver.MySQLDriver$"
lagom.persistence.jpa {
persistence-unit = "default"
initialization-retry {
interval {
min = 5s
factor = 1.0
}
max-retries = 10
}
}
persistence.xml
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.2"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQLDialect"/>
<!-- Add any other standard or provider-specific properties -->
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/test" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="root" />
<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" />
</properties>
</persistence-unit>
</persistence>
I'm using the following maven dependencies
<dependency>
<groupId>com.lightbend.lagom</groupId>
<artifactId>lagom-javadsl-persistence-jdbc_${scala.binary.version}</artifactId>
<version>${lagom.version}</version>
</dependency>
<dependency>
<groupId>com.lightbend.lagom</groupId>
<artifactId>lagom-javadsl-persistence-jpa_${scala.binary.version}</artifactId>
<version>${lagom.version}</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.0.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
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 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.