Inheritance is a fundamental concept of object oriented programming like Java. But relational databases have no concept of inheritance. To overcome this mismatch, JPA provides different strategies so that we can map inheritance class hierarchies to database tables. Those strategies…
Delete JPA Entity
In many situations you may want to delete an existing entity object from the database. For this, you can use the remove() method provided by EntityManager. This may also delete associated entities as a result of a cascade operation. Embedded…
JPA FetchType
Sometimes you have two entities and they have a relationship between them. For example, you might have an entity called State and another entity called City where a State can have many Cities. Here the association between State and City…
Mapping entity association
Often the first step to develop an application is to create the entities and define the relationships or associations between them. A relationship basically means that one entity holds a reference to another entity. For example, consider the following User…
JPA Lifecycle events, mapping special data types
Before understanding the JPA lifecycle events, let’s understand how to map temporal types. Mapping temporal types Most databases support a few different temporal data types with different levels of accuracy. As per JPA specification, you should annotate persistent fields of…
JPA Query API
We already know that the EntityManager.find() method is used to retrieve entities by their primary key. However, there may be many situations where you need to search by other columns. The Query API allows us to create and execute custom…
Entity life cycle
An entity instance goes through different states based on the operation we are performing. We refer to this as the entity life cycle. Instance of an entity can be in one of four states – Transient or New Persistent or…
EntityManager
As we discussed earlier, we use @Entity annotation to mark a class as an entity and use several other annotations to map an entity with table and entity attributes with column. EntityManager is used in an application to persist, search…
Generate Identifiers in Hibernate
As we discussed earlier, every entity class must have an identifier attribute which corresponds to the primary key column of the table. This attribute must be annotated with @Id. The field to which this @Id annotation is applied should be…
Introduction to Hibernate and JPA
What is data persistence? Data Persistence is a way for an application to store the data to a non volatile storage so that it can survive even after the process with which it was created has ended. Consider non volatile…