JPA Interview Questions and Answers
Ques 6. What is Embeddable classes?
Ans. Entities may use persistent fields, persistent properties, or a combination of both. If the mapping annotations are applied to the entity’s instance variables, the entity uses persistent fields. If the mapping annotations are applied to the entity’s getter methods for JavaBeans-style properties, the entity uses persistent properties.
Ques 7. What is Persistent Fields?Ans. If the entity class uses persistent fields, the Persistence runtime accesses entity-class instance variables directly. All fields not annotated javax.persistence.Transient or not marked as Java transient will be persisted to the data store. The object/relational mapping annotations must be applied to the instance variables.
Ques 8. What is Persistent Properties?Ans. If the entity uses persistent properties, the entity must follow the method conventions of JavaBeans components. JavaBeans-style properties use getter and setter methods that are typically named after the entity class’s instance variable names. For every persistent property property of type Type of the entity, there is a getter method getProperty and setter method setProperty. If the property is a Boolean, you may use isProperty instead of getProperty. For example, if a Customer entity uses persistent properties and has a private instance variable called firstName, the class defines a getFirstName and setFirstName method for retrieving and setting the state of the firstName instance variable.
The method signature for single-valued persistent properties are as follows:
void setProperty(Type type)
The object/relational mapping annotations for persistent properties must be applied to the getter methods. Mapping annotations cannot be applied to fields or properties annotated @Transient or marked transient.
Ques 9. Explain Life Cycle of a JPA Entity.Ans. Key states that an entity might be in:
1) New / Transient: An object is instantiated but not yet associated with an Entity Manager and has no representation in the database.
2) Managed / Persisted.
3) Detached: Detached entity objects are objects in a special state in which they are not managed by any EntityManager but still represent objects in the database. Detached objects are often returned from a persistence tier to the web layer where they can be displayed to the end-user in some form. Changes can be made to a detached dobject, but these changes won\'t be persisted to the database until the entity is reassociated with a persistence context (the entity is merged back to an EntityManager to become managed again).
- The merge method\'s major task is to transfer the state from an unmanaged entity (passed as the argument) to its managed counterpart within the persistence context.
- merge deals with both new and detached entities. Merge causes either INSERT or UPDATE operation according to the sub-scenario (on the one hand it is more robust, on the other hand this robustness needn\'t be required.)
- persist always causes INSERT SQL operation is executed (i.e. an exception may be thrown if the entity has already been inserted and thus the primary key violation happens.)
Ques 10. What is JPQL?Ans. JPQL (Java Persistence Query Language) offers an object-oriented syntax for expressing query that is very similar to SQL. The language is interpreted at runtime, which means you cannot use the compiler to verify the correctness and integrity of a query. To adress this limitation, Hibernate includes a Criteria API, which allows queries to be expressed programmatically.
Most helpful rated by users: