JPA Fundamentals & Hibernate - 10) Inheritance Strategies
In this article, we are going to look how to represent the inheritance relationship in the JPA and how to use @MapperSuperclass annotation Github Link …
In this article, we are going to discuss what is the entity manager in the JPA and also learn the basic operations in the entity manager
To obtain an entityManager, you must use factory pattern. For all the previous examples, we always used the factory pattern to obtain an EntityManager:
public class Main {
public static final String PERSISTENCE_UNIT = "postgresqlPersistenceUnit";
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
EntityManager entityManager = entityManagerFactory.createEntityManager();
// ...
}
Entity Manager is responsible to manage entities. What it manages is the context of the entities.
JPA implementation is creating own its context. Context is actually collection of instances. Those instances are some type of managed objects. For the JPA, we call it “entities”.
I will not give examples for all the operations. And actually you won’t need all the operations especially if use framework such as Spring Boot.
Use this operation to make instance part of a context. In other words, to make instance to be managed by JPA.
Synchronize the context to the underlying database.
To retrieve instance of an entity from the database by using the primary key
Similar to find()
but this operation doesn’t give all the details in the beginning.
Check if the instance is or not in the context
Remove the given entity from the context
Clear the context. In other word, detach all the instances from the context.
Remove the entity instance from the context and the persistence layer.
Merge the state of the given entity into the current context.
Refresh the state of the instance from the database, overwriting changes made to the entity, if any.
Use flush()
if we want to reflect the context as soon as possible.
When we are using persis(Object entity)
, instance will reflected to the entity after commit the transaction.
See the examples:
public class Main {
public static void main(String[] args) {
try {
entityManager.getTransaction().begin();
entityManager.persist(...);
System.out.println("This will be run before commit");
entityManager.getTransaction().commit();
}catch (Exception e) {
e.printStackTrace();
} finally {
entityManager.close();
}
}
}
// console output:
// This will be run before commit
// Hibernate: insert into Cart (name, id) values (?, ?)
public class Main {
public static void main(String[] args) {
try {
entityManager.getTransaction().begin();
entityManager.persist(...);
entityManager.flush();
System.out.println("This will be run before commit");
entityManager.getTransaction().commit();
}catch (Exception e) {
e.printStackTrace();
} finally {
entityManager.close();
}
}
}
// console output:
// Hibernate: insert into Cart (name, id) values (?, ?)
// This will be run before commit
As we can see, flush will run the insert sql query before the commit method.
getReference()
is a kind of lazy loading version of the find()
method.
When we get the entity with the find()
, Hibernate will run the select query at the beginning. However when we use getReference()
until we use returned object somewhere else (such as returnedObject.getId()
) hibernate will not run the select query.
In other words, getReference()
returns the proxy of the entity and until you use proxy, hibernate will not run any select query.
Last but not least, wait for the next one …
In this article, we are going to look how to represent the inheritance relationship in the JPA and how to use @MapperSuperclass annotation Github Link …
In this article, we are going to learn how to create relationship using @Embeddable & @Embedded object . @AssociativeOverride is used to override …