Top Java Persistence API(JPA) frequently asked interview questions.
Q: What is JPA?
A: Java Persistence API is a collection of classes and methods to persistently store the vast amounts of data into a database which is provided by the Oracle Corporation.
The Java Persistence API (JPA) is a Java application programming interface specification that describes the management of relational data in applications using Java Platform, Standard Edition and Java Platform, Enterprise Edition. Persistence in this context covers three areas:
- the API itself, defined in the javax.persistence package
- the Java Persistence Query Language (JPQL)
- object/relational metadata
Q: What's the difference between JPA and Hibernate ?
A:JPA is the interface while Hibernate is the implementation.
There are multiple popular implementations of JPA.
- Hibernate
- MyBatis
- TopLink
A: Both persist and merge serve different purposes.
Persist | Merge |
---|---|
Persist takes an entity instance, adds it to the context and makes that instance managed | Merge creates a new instance of your entity, copies the state from the supplied entity, and makes the new copy managed. |
Insert a new register to the database | Find an attached object with the same id and update it. |
You want the method always creates a new entity and never updates an entity. Otherwise, the method throws an exception as a consequence of primary key uniqueness violation. | You want the method either inserts or updates an entity in the database. |