Tomek Tomek - 3 months ago 30
Java Question

Use Hibernate Core instead of EnityManager

I was using Hibernate EntityManager dependecy in my earlier projects and it was working fine. Now I see it is deprecated in favour of Hibernate Core. However my project doesn't work with that dependency (it works with hibernate-entitymanager).

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>HibernateDocs_1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.2.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>


Im executing this code:

public class NewClass {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("one");
emf.close();
}
}


and I get:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named one


JPA config:

<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="one">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/Hibernate" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="but" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />

<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</persistence>


Location of persistence.xml:

project structure

Answer

You're still using the old provider.
Switch to the new one

<persistence-unit name="one">
   <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
   ...