Object-relational Mapping Using JPA, Hibernate and Spring Data JPA. Switching between JP and Hibernate
Object-relational Mapping Using JPA, Hibernate and Spring Data JPA. Switching between JP and Hibernate
Another article in our series. In this one we talk about switching between JP and Hibernate.
20 груд 2021
676
Інші статті
How to incrementally migrate the data from RDBMS to Hadoop using Sqoop Incremental Last Modified technique?
How to implement Slowly Changing Dimensions(SCD) Type 2 in Spark?
How to incrementally migrate the data from RDBMS to Hadoop using Sqoop Incremental Append technique?
Why MongoDB don't fetch all the matching documents for the query fired
How to solve the issue of full disk utilization in HDFS Namenode
Can We Use HDFS as Back-up Storage?
How to do Indexing in MongoDB with Elastic Search? Part 1
How to do Indexing in MongoDB with Elastic Search? Part 2
How to store data on browser using NoSQL IndexedDB?
How to Apply MBTI in HR: Motivation for every day. Groups of People & their Motivations
It is possible that we are working with JPA and we need to access the Hibernate API. Or, vice-versa, we work with Hibernate native and we need to create an EntityManagerFactory from the Hibernate configuration.
To obtain a SessionFactory from an EntityManagerFactory, we’ll have to unwrap the first one from the second one.
Listing 7 Obtaining a SessionFactory from an EntityManagerFactory
private static SessionFactory getSessionFactory
(EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.
unwrap(SessionFactory.class);
private static EntityManagerFactory
createEntityManagerFactory() {
Configuration configuration = new
Configuration(); #1
configuration.configure().
addAnnotatedClass(Item.class); #2
Map properties =
new HashMap<>(); #3
Enumeration propertyNames =
configuration.getProperties().
propertyNames(); #4
while (propertyNames.hasMoreElements()) {
String element = (String)
propertyNames.nextElement(); #5
properties.put(element,
configuration.getProperties().
getProperty(element)); #5
}
return Persistence.
createEntityManagerFactory("cscs",
properties); #6
}
Interested in learning how to program with Java or in upgrading your Java programming skills? Check out our trainings
Catalin Tudose
Java and Web Technologies Expert
To obtain a SessionFactory from an EntityManagerFactory, we’ll have to unwrap the first one from the second one.
Listing 7 Obtaining a SessionFactory from an EntityManagerFactory
private static SessionFactory getSessionFactory
(EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.
unwrap(SessionFactory.class);
}
Starting with JPA version 2.0, we may get access to the APIs of the underlying implementations. The EntityManagerFactory (and also the EntityManager) declare an unwrap method that will return objects belonging to the classes of the JPA implementation. When using the Hibernate implementation, we may get the corresponding SessionFactory or Session objects.
We may be interested in the reverse operation: create an EntityManagerFactory from an initial Hibernate configuration.
Listing 8 Obtaining an EntityManagerFactory from a Hibernate configuration
createEntityManagerFactory() {
Configuration configuration = new
Configuration(); #1
configuration.configure().
addAnnotatedClass(Item.class); #2
Map properties =
new HashMap<>(); #3
Enumeration propertyNames =
configuration.getProperties().
propertyNames(); #4
while (propertyNames.hasMoreElements()) {
String element = (String)
propertyNames.nextElement(); #5
properties.put(element,
configuration.getProperties().
getProperty(element)); #5
}
return Persistence.
createEntityManagerFactory("cscs",
properties); #6
}
- We create a new Hibernate configuration #1, then call the configure method, which adds the content of the default hibernate.cfg.xml file to the configuration.
- We explicitly add Item as annotated class #2.
- We create a new hash map to be filled in with the existing properties #3.
- We get all property names from Hibernate configuration #4, then we add them one by one to the previously created map #5.
- We return a new EntityManagerFactory, providing to it the cscs persistence unit name and the previously created map of properties #6.
Interested in learning how to program with Java or in upgrading your Java programming skills? Check out our trainings
Catalin Tudose
Java and Web Technologies Expert