I have my persistence.xml
with the same name using TopLink
under the META-INF
directory.
Then, I have my code calling it with:
After <persistence-unit name="agisdb">
, define the persistence provider name:
<provider>org.hibernate.ejb.HibernatePersistence</provider>
In an OSGi-context, it's necessary to list your persistence units in the bundle's MANIFEST.MF, e.g.
JPA-PersistenceUnits: my-persistence-unit
Otherwise, the JPA-bundle won't know your bundle contains persistence units.
See http://wiki.eclipse.org/EclipseLink/Examples/OSGi/Developing_with_EclipseLink_OSGi_in_PDE .
You have to use the absolute path of the file otherwise this will not work. Then with that path we build the file and pass it to the configuration.
@Throws(HibernateException::class)
fun getSessionFactory() : SessionFactory {
return Configuration()
.configure(getFile())
.buildSessionFactory()
}
private fun getFile(canonicalName: String): File {
val absolutePathCurrentModule = System.getProperty("user.dir")
val pathFromProjectRoot = absolutePathCurrentModule.dropLastWhile { it != '/' }
val absolutePathFromProjectRoot = "${pathFromProjectRoot}module-name/src/main/resources/$canonicalName"
println("Absolute Path of secret-hibernate.cfg.xml: $absolutePathFromProjectRoot")
return File(absolutePathFromProjectRoot)
}
GL
Source
Hibernate 5.2.5
Jar Files Required in the class path. This is within a required folder of Hibernate 5.2.5 Final release. It can be downloaded from http://hibernate.org/orm/downloads/
Create an xml file "persistence.xml" in
YourProject/src/META-INF/persistence.xml
persistence.xml
<?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="sample">
<class>org.pramod.data.object.UserDetail</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/hibernate_project"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.cache.use_second_level_cache" value="false"/>
<property name="hibernate.archive.autodetection" value="true"/>
</properties>
</persistence-unit>
EntityManagerFactor = Persistance.createEntityManagerFactory("sample");. "sample" can be changed as per your naming convention.
Now create a Entity class. with name as per my example UserDetail, in the package org.pramod.data.object
UserDetail.java
package org.pramod.data.object;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "user_detail")
public class UserDetail {
@Id
@Column(name="user_id")
private int id;
@Column(name="user_name")
private String userName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Override
public String toString() {
return "UserDetail [id=" + id + ", userName=" + userName + "]";
}
}
Now create a class with main method.
HibernateTest.java
package org.pramod.hibernate;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.pramod.data.object.UserDetail;
public class HibernateTest {
private static EntityManagerFactory entityManagerFactory;
public static void main(String[] args) {
UserDetail user = new UserDetail();
user.setId(1);
user.setUserName("Pramod Sharma");
try {
entityManagerFactory = Persistence.createEntityManagerFactory("sample");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist( user );
entityManager.getTransaction().commit();
System.out.println("successfull");
entityManager.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output will be
UserDetail [id=1, userName=Pramod Sharma]
Hibernate: drop table if exists user_details
Hibernate: create table user_details (user_id integer not null, user_name varchar(255), primary key (user_id))
Hibernate: insert into user_details (user_name, user_id) values (?, ?)
successfull
In my case it was about mistake in two properties as below. When I changed them ‘No Persistence provider for EntityManager named’ disappered.
So you could try test connection with your properties to check if everything is correct.
<property name="javax.persistence.jdbc.url" value="...”/>
<property name="javax.persistence.jdbc.password" value="...”/>
Strange error, I was totally confused because of it.
I also had this error but the issue was the namespace uri in the persistence.xml
.
I replaced http://xmlns.jcp.org/xml/ns/persistence
to http://java.sun.com/xml/ns/persistence
and the version 2.1 to 2.0.
It's now working.