Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

JPA Interview Questions and Answers

Related differences

JDBC vs JPAJPA vs Hibernate

Ques 11. What is Persistent Fields?

  • 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.

Is it helpful? Add Comment View Comments
 

Ques 12. Is it possible to use JPA with NoSQL databases?

In general, the JPA specification says only about mapping java objects into relational database tables, but there are a number of implementations of this standard for NoSQL databases: Kundera, DataNucleus, ObjectDB, and a number of others. Naturally, not all specification-specific features for relational databases are transferred to NoSQL databases completely.

Is it helpful? Add Comment View Comments
 

Ques 13. What JPA requirements for Entity classes can you list?

1) Entity class must be annotated with Entity or described in the XML configuration file JPA,

2) Entity class must contain a public or protected constructor with no arguments (it can also have constructors with arguments),

3) Entity class must be a top-level class (top-level class),

4) Entity class cannot be an enum or interface,

5) Entity class cannot be the final class,

6) Entity class cannot contain final fields or methods if they participate in the mapping (persistent final methods or persistent final instance variables),

7) If an Entity class object is passed by value as a separate object (detached object), for example through a remote interface (through a remote interface), it must also implement a Serializable interface,

8) The Entity class fields should be directly accessible only to the methods of the Entity class and should not be directly accessible to other classes using this entity. Such classes should refer only to methods (getter/setter methods or other business logic methods in the Entity class),

9) The Entity class must contain a primary key, that is, an attribute or group of attributes that uniquely defines the record of this Entity class in the database.

Is it helpful? Add Comment View Comments
 

Ques 14. What is Persistent Properties?

  • 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 of type Type of the entity, there is a getter method getProperty and a 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:

    Type getProperty()
    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.

Is it helpful? Add Comment View Comments
 

Ques 15. Explain Life Cycle of a JPA Entity.

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 object, but these changes won't be persisted in the database until the entity is reassociated with a persistence context (the entity is merged back to an EntityManager to become managed again).
  4. Removed.

    - 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.)

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook