Cannot configure hibernate EntityManager configuration with .xml file - java

I tried to write some code with EntityManager but hibernate was updated to hibernate-core(6.0.0.Final) and with new hibernate 6.0 my old codes doesn't work
There my code:
my pom.xml
enter image description here
my persistence.xml file
<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="CRM">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5678/postgres"/>
<property name="hibernate.connection.username" value="postgres"/>
<property name="hibernate.connection.password" value="postgres"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
and my main method
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("CRM");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(new SuperHero());
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
here result
enter image description here
thanks in advance for your help

It looks as if you are mixing 2 incompatible versions of Hibernate resources:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.0.0.Final</version>
</dependency>
and:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.6.7.Final</version>
</dependency>
From v6 onwards, the Hibernate libraries have moved from using Java Persistence to Jakarta Persistence. You can read about this larger Java ecosystem change elsewhere Transition from Java EE to Jakarta EE - and also in other SO questions and answers.
By including a Hibernate Entity Manager v5 dependency, your project will still be referring to a Java Persistence library (e.g. via javax.persistence-api-2.2.jar or similar). This means your code may still compile - but, as you see, it will not execute. You will see error messages referring to javax classes, which are no longer supported by the v6 Hibernate Core library.
Furthermore, Hibernate's JPA support has been merged into the hibernate-core module, making the hibernate-entitymanager module obsolete. You can see a note about this by looking at the readme.txt file in your Entity Manager 5.6.7 JAR file:
Hibernate's JPA support has been merged into the hibernate-core module, making this hibernate-entitymanager module obsolete. This module will be removed in Hibernate ORM 6.0.
Recommended steps:
Remove the hibernate-entitymanager dependency from your POM. That will probably trigger a series of compilation errors, because you will no longer have any library support for classes such as javax.persistence.EntityManager.
Update all your javax imports to jakarta imports. So, for example, taking the class from (1) above, that becomes:
import jakarta.persistence.EntityManager;
In your persistence.xml file you will also need to fix any similar references to javax - for example:
<property name="jakarta.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver" />
Final Notes
If you still face issues following the above steps, then you can refer to the official Hibernate ORM 6.0 Migration Guide.

Related

No Persistence provider for EntityManager named- when try to deploy from weblogic console

I'm struggling for a few days about this error.
When I try to deploy from ide(IntelliJ-eclipse), it works correct.
But when I try to deploy from WebLogic console, I got these errors;
Error Unable to access the selected application.
Error Unable to invoke Annotation processoror.
Error Unable to invoke Annotation processoror.
For more information, I checked logs and errors from stack trace.
It looks like the main error is: javax.persistence.PersistenceException: No Persistence provider for EntityManager named.
In debug, throwing from this code; Persistence.createEntityManagerFactory("persistanceUnitName");
Persistence classes are coming from javaee-web-abi-7.0.jar.
Everything looks fine in 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="persistanceUnitName"
transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>dataSource</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.target-database" value="Oracle" />
<property name="eclipselink.logging.parameters" value="true" />
<property name="eclipselink.logging.logger" value="DefaultLogger" />
<property name="eclipselink.logging.level" value="WARNING" />
<property name="eclipselink.refresh" value="true" />
<property name="eclipselink.query-results-cache" value="false" />
<!-- <property name="eclipselink.ddl-generation" value="create-or-extend-tables" />-->
</properties>
</persistence-unit>
</persistence>
persistance.xml located in /WEB-INF/classes/META-INF
We got a case like that: we have a test and prep environment. In test, it works, in prep doesn't work, in local doesn't work (from WebLogic console). Unfortunately, I can't see test server configs. But I expect the same options with prep. Anyway forget other environments, firstly I need to deploy successful from local.
Could you pls help me, I really don't know what I miss. Read every topic, tried everything
It looks like a classpath issue. Check your weblogic classpath.
If you are using eclipselink library must be loaded first when you are deploying.
Check this.
Add eclipselink into your project with scope provided:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.0</version>
<scope>provided<scope>
</dependency>
add same dependency into your weblogic classpath:
$WEBLOGIC_HOME/user_projects/domains/base_domain/lib
Then, add below to your weblogic.xml to use eclipselink library.
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>org.eclipse.persistence.*</wls:package-name>
</wls:prefer-application-packages>
</wls:container-descriptor>

cannot resolve org.apache.openjpa.persistence.PersistenceProviderImpl

I keep getting an error saying:
cannot resolve org.apache.openjpa.persistence.PersistenceProviderImpl
My jar file is there but I am having a hard time trying to figure out why I am still getting the error:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="dataBase">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<properties>
<property name="openjpa.ConnectionURL" value="com.mysql.jdbc.Driver"/>
<property name="openjpa.ConnectionDriverName" value="jdbc:mysql://localhost:8080/springpractice"/>
<property name="openjpa.ConnectionUserName" value="root"/>
<!--<property name="openjpa.ConnectionPassword" value=""/>-->
<property name="openjpa.Log" value="DefaultLevel=TRACE, Tool=INFO"/>
</properties>
</persistence-unit>
</persistence>
I have look all over google and the answer is not there. I double check to make sure the dependencies are not there twice in case there was a conflict. Because of this when I try to declare a #Entity it is not picking up the database tables.
Based on the images of your lib directory, I would say you the XML definition for persistence-unit>provider is not as expected.
There are Hibernate Jars in your lib, so it seem like that
"Persistence Provider" should be "Hibernate", and not "Apache OpenJPA".
If you really want to use Apache OpenJPA, then I would recommend removing Hibernate Jar files in your lib.
Hibernate is more popular of the two, so I would recommend it.
You have to import a JPA implementation jar (OpenJPA in this case). You could get one from Maven Central or Apache OpenJPA home page itself.
UPDATED: as it has been mentioned in another ansewer it's better to remove the other JPA provider libraries (Hibernate).
I had to add the following jar:
org.apache.openjpa:com.springsource.org.apache.openjpa.persistence:1.02
I trusted my IDE to much that it would download all the jars that I needed.

Hibernate 4.3.6+ HikariCP Implementation

I am trying to use HikariCP with Hibernate in a maven java ee web app in Netbeans. This is my first time using Hibernate, and I am not using Spring yet as I am not familiar with it but may consider it in the future (please feel free to offer reason why I should adapt it).
In the HikariCP wiki, it mentions that:
As of Hibernate 4.3.6 there is an official ConnectionProvider class
from Hibernate, which should be used instead of the HikariCP
implementation. The class is called
org.hibernate.hikaricp.internal.HikariCPConnectionProvider.
It then goes on to describe the configuration for Hibernate 4.x. It talks about the old ConnectionProvider class there however so I am assuming this section is outdated?
I have added the following dependency to my POM.XML file:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-hikaricp</artifactId>
<version>4.3.8.Final</version>
</dependency>
and with the following properties declared within the tag in hibernate.cfg.xml:
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.provider_class">org.hibernate.hikaricp.internal.HikariCPConnectionProvider</property>
<property name="hibernate.hikari.dataSource.url">jdbc:mysql://localhost/testdb?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.hikari.dataSource.user">testuser</property>
<property name="hibernate.hikari.dataSource.password">password</property>
<property name="hibernate.hikari.dataSourceClassName">com.mysql.jdbc.jdbc2.optional.MysqlDataSource</property>
<property name="hibernate.hikari.dataSource.cachePrepStmts">true</property>
<property name="hibernate.hikari.dataSource.prepStmtCacheSize">250</property>
<property name="hibernate.hikari.dataSource.prepStmtCacheSqlLimit">2048</property>
<property name="hibernate.hikari.dataSource.useServerPrepStmts">true</property>
<property name="hibernate.current_session_context_class">thread</property>
I am able to interact with the database as I was before, using the default Hibernate connection pool. Is this all I need to do? Is it now using the HikariCP connection pool? If not what other configuration is required?
If I were to later integrate Spring, how would this affect the HikariCP configuration, or location of the configuration?

javax.persistence.PersistenceException: No Persistence provider for EntityManager named customerManager

I am new to JPA & Hibernate. After reading some online materials I now understand what Hibernate is and how it can be used with JPA.
Now, I am trying to run this JPA & Hibernate tutorial. I've done everything they mention in this tutorial.
I don't have Oracle DB, only MySQL. So I made some changes to persistence.xml using my understanding of JPA & Hibernate (I don't know if it's correct or not... Seems to me it is.)
Here is my persistence.xml
<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="customerManager" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Customer</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="1234"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/general"/>
<property name="hibernate.max_fetch_depth" value="3"/>
</properties>
</persistence-unit>
</persistence>
But I don't seem to get the output they describe. It's giving me:
Customer id before creation:null
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named customerManager
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
at CustomerDAO.create(CustomerDAO.java:8)
at CustomerDAO.main(CustomerDAO.java:22)
Any suggestions will be appreciated.
Update:
I have made the changes that are asked to done. But, still getting the asme error lines!!!
They didnt mentioned anything about orm.xml in that tutorial. may it be a problem causer!!!
Just for completeness. There is another situation causing this error:
missing META-INF/services/javax.persistence.spi.PersistenceProvider
file.
For Hibernate, it's located in hibernate-entitymanager-XXX.jar, so, if hibernate-entitymanager-XXX.jar is not in your classpath, you will got this error too.
This error message is so misleading, and it costs me hours to get it correct.
See JPA 2.0 using Hibernate as provider - Exception: No Persistence provider for EntityManager.
Your persistence.xml is not valid and the EntityManagerFactory can't get created. It should be:
<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="customerManager" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Customer</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="1234"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/general"/>
<property name="hibernate.max_fetch_depth" value="3"/>
</properties>
</persistence-unit>
</persistence>
(Note how the <property> elements are closed, they shouldn't be nested)
Update: I went through the tutorial and you will also have to change the Id generation strategy when using MySQL (as MySQL doesn't support sequences). I suggest using the AUTO strategy (defaults to IDENTITY with MySQL). To do so, remove the SequenceGenerator annotation and change the code like this:
#Entity
#Table(name="TAB_CUSTOMER")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="CUSTOMER_ID", precision=0)
private Long customerId = null;
...
}
This should help.
PS: you should also provide a log4j.properties as suggested.
I had the same problem today. My persistence.xml was in the wrong location. I had to put it in the following path:
project/src/main/resources/META-INF/persistence.xml
I was facing the same issue. I realised that I was using the Wrong provider class in persistence.xml
For Hibernate it should be
<provider>org.hibernate.ejb.HibernatePersistence</provider>
And for EclipseLink it should be
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
If you use Hibernate 5.2.10.Final, you should change
<provider>org.hibernate.ejb.HibernatePersistence</provider>
to
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
in your persistence.xml
According to Hibernate 5.2.2: No Persistence provider for EntityManager
If you are using Maven you may have both src/{main,test}/resources/META-INF/persistence.xml. This is a common setup: test your JPA code with h2 or Derby and deploy it with PostgreSQL or some other full DBMS. If you're using this pattern, do make sure the two files have different unit names, else some versions of the Persistence class will try to load BOTH (because of course your test-time CLASSPATH includes both classes and test-classes); this will cause conflicting definitions of the persistence unit, resulting in the dreaded annoying message that we all hate so much!
Worse: this may "work" with some older versions of e.g., Hibernate, but fail with current versions. Worth getting it right anyway...
A bit too late but I got the same issue and fixed it switching schemalocation into schemaLocation in the persistence.xml file (line 1).
I have seen this error , for me the issue was there was a space in the absolute path of the persistance.xml , removal of the same helped me.
I was also facing the same issue when I was trying to get JPA entity manager configured in Tomcat 8. First I has an issue with the SystemException class not being found and hence the entityManagerFactory was not being created. I removed the hibernate entity manager dependency and then my entityManagerFactory was not able to lookup for the persistence provider. After going thru a lot of research and time got to know that hibernate entity manager is must to lookup for some configuration. Then put back the entity manager jar and then added JTA Api as a dependency and it worked fine.
my experience tells me that missing persistence.xml,will generate the same exception too.
i caught the same error msg today when i tried to run a jar package packed by ant.
when i used jar tvf to check the content of the jar file, i realized that "ant" forgot to pack the persistnece.xml for me.
after I manually repacked the jar file ,the error msg disappered.
so i believe maybe you should try simplely putting META-INF under src directory and placing your persistence.xml there.

JPA Exception: No Persistence provider for EntityManager named MyJPAApplicationPU

I am newbie with JPA. I'm trying to run some sample code using JPA but I get the following exception:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named MyJPAApplicationPU
I put my exception message here,
INFO: Could not find any META-INF/persistence.xml file in the classpath
javax.persistence.PersistenceException: No Persistence provider for EntityManager named MyJPAApplicationPU
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
at com.myJpa.TestJPA.setUp(TestJPA.java:30)
at com.myJpa.TestJPA.main(TestJPA.java:72)
Any help would be appreciated.
Well, the error is self explaining, you need to provide a META-INF/persistence.xml to use JPA. This file is used to define a "persistence unit". From the JPA 1.0 specification:
6.2.1 persistence.xml file
A persistence.xml file defines a
persistence unit. It may be used to
specify managed persistence classes
included in the persistence unit,
object/relational mapping information
for those classes, and other
configuration information for the
persistence unit and for the entity
manager(s) and entity manager factory
for the persistence unit. The
persistence.xml file is located in
the META-INF directory of the root
of the persistence unit. This
information may be defined by
containment or by reference, as
described below.
Here is a sample persistence.xml for a Java SE environment (using Hibernate as JPA provider):
<?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="MyJPAApplicationPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.mycompany.Foo</class>
<class>com.mycompany.Bar</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.DriverManagerConnectionProvider" />
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:unit-testing-jpa"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
Some comments about the above file:
when running in a Java SE environment, you cannot rely on JTA and the transaction type must be RESOURCE_LOCAL (which is actually the default in a Java SE environment but specifying it make it more clear).
when running in a Java SE environment, you cannot use a JDNI data source and the provider will obtain connections directly from the JDBC driver so you must pass the relevant informations to the provider (driver class name, connection url, user, password). With JPA 1.0 the properties used to pass these metadata are provider specific.
To insure the portability of a Java SE application, it is necessary to explicitly list the managed persistence classes that are included in the persistence unit.
For JPA to work, you need META-INF/persistence.xml. I will assume this is a web-app, so this folder has to be in WEB-INF/classes/.
The persistence.xml file would look like this:
<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="MyJPAApplicationPU" transaction-type="RESOURCE_LOCAL">
</persistence-unit>
</persistence>
EntityManagerFactory emf = Persistence.createEntityManagerFactory("<JDBC connection>");
Check the correct JDBC Connection.

Categories

Resources