JPA Interview Questions and Answers (2022)
- Q: What is JPA?
- Q: What is Java Object-Relational Mapping (ORM)?
- Q: What is difference between JPA and Hibernate?
- Q: What are different ORM frameworks?
- Q: What is the JPQL?
- Q: What is embeddable class in JPA?
- Q: What is the difference between persist and merge in jpa?
- What are the different types of entity mapping?
- What is the role of Entity Manager in JPA?
Q: What is JPA?
The Java Persistence API (JPA) is a Java Object-Relational Mapping (ORM) Standard. Using JPA, the developer can map, store, update and retrieve data from relational databases to Java objects, and vice versa.Refer Spring Boot Complete JPA CRUD example for implementation.
Q: What is Java Object-Relational Mapping (ORM)?
Object-relational mapping is a technique used to develop and maintain a relationship between an object and a relational database by mapping an object state into a database column. It translates the programming code attributes to the columns in the table. It is good at managing various database operations effectively, such as insertion, update, deletion, etc.
Q: What is difference between JPA and Hibernate?
JPA is the interface while Hibernate is the implementation.
There are various popular JPA implementations (JPA Provider).
Using JPA + JPA Provider(Hibernate): The benefit of JPA is that it allows you to switch out your implementation if necessary. So migration is easily possible in JPA.
The downside is that the native hibernate/toplink/etc. The API can give functionality that the JPA specification does not support.
Using Only Hibernate: It uses only Hibernate which does not need any interface, used very less as when you are not going change your JPA Provider at any cost.
Take a look at our Suggested Posts :
Q: What are different ORM frameworks?
Following are the some ORM frameworks:
Q: What is the JPQL?
JPQL (Java Persistence Query Language) is an object-oriented query language used to perform database operations on persistent entities. JPQL uses the entity object model instead of the database table to execute SQL queries.
Q: What is the difference between persist and merge in jpa?
|Persist should be used only on new entities, to add them to DB (if entity already exists in DB there will be EntityExistsException throw).||Merge operation is used to insert an entity into the context or update information about an entity already managed and the obtained response will be the managed entity.|
|Insert a new register to the database.||Find an attached object with the same id and update it.|
|It always make a new entity and never updates an entity. Otherwise, it throws an exception (EntityExistsException) as a consequence of primary key uniqueness violation.||It inserts or updates an entity in the database.|
Q: What is embeddable class in JPA?
- An entity may have references of other non-entity classes. Such non-entity classes are referred as embeddable classes.
- All fields in the embeddable class are mapped to the owner entity table.
- @Embeddable annotation is used for a non-entity class. This is the class that needs to be embedded in an entity class.
- Embeddable class and it's fields/properties will use all annotations used by an entity (e.g. @Column etc), other than @Entity annotation.
- @Embedded is used in the entity class. This annotation is put on a field/property applies to an embeddable class.
- Embedded objects belong strictly to their owning entity, and should not be shared across persistent entities.
Q: What are the different types of entity mapping? Ans:
Following are the types of object-relational mapping:
- One-to-one mapping: This mapping is a single-value association where an instance of one entity is associated with an instance of another entity. Which indicates that one instance of the source entity can be mapped to at most one instance of the target entity.
- One-To-Many mapping: This mapping falls under the category of collection-valued association where an entity is associated with a collection of other entities. This implies that the instance of one entity can be mapped with any number of instances of another entity.
- Many-to-one mapping: This mapping a single-valued association in which a collection of entities can be associated with the similar entity. This implies that more than one row of an entity can refer to the same row of another entity.
- Many-to-many mapping: This mapping is a collection-valued association in which any number of entities can be associated with a collection of other entities. This means that more than one row of one entity can refer to more than one row of another entity.