In most of the enterprise application, multiple users can access the application concurrently. Suppose two users (User1 & User2) are trying to work on the same database record. Initially when they will fetch the record, it will be same for…
Tag: JPA
Caching Data
Accessing a database is an expensive operation and it takes time. When an application sends a request to the database, the request is first sent over the network. Then the request is processed by the database and finally the result…
Mapping inheritance
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…