Java Persistance API (JPA) - Interview Questions

What is the role of Java Persistance API (JPA) in Java EE?

 FAQ

The Java Persistance API (JPA) provides the capability of object relational mapping for managing relational data in Java applications.

The Java Persistance API provides features for mapping relational data to Java objects or entities, managing the entities, querying the entities and also in creating the database schema.

What are Entity classes in JPA? What are the requirements for an Entity class?

 FAQ

An Entity class is a lightweight persistence class, that is typically mapped to a relational table. An Entity class is decorated with the annotation javax.persistence.Entity. Each Entity class instance corresponds to a row in a relational table.

Fields or properties in an entity class are mapped to the relational data by decorating them with annotations.

What are the requirements for an Entity class?

 FAQ

An Entity class must follow the following requirements.

  • The class must be annotated with the javax.persistence.Entity annotation.
  • The class must have a public or protected no-argument constructor
  • The Entity class, it's methods and persistent variables should not be declared final.
  • The persistent instance variables must be declared as private, protected or default (package-private). They can only be accessed through accessor methods.
  • The Entity class must implement the Serializable interface if it's instance needs to be passed as value.
  • Entities can extend from either another Entity class or a Non-Entity class.

@Entity
public class Order { ... }

What is the annotation used for persistence fields of type Collection in an Entity class?

 FAQ

Persistent fields in an Entity class which are of type Collection are annotated with the javax.persistence.ElementCollection annotation.

@Entity
public class Order {
 @ElementCollection
 protected List orderLines = new ArrayList<>();
}

For persistent fields of type collection, how do you specify if the collection elements have to be loaded eagerly or lazily?

 FAQ

The javax.persistence.ElementCollection has an attribute fetch which can be specified with two values either 'LAZY' or 'EAGER'

//Load order lines eagerly
@Entity
public class Order {
 @ElementCollection(fetch=EAGER)
 protected List orderLines = new ArrayList<>();
}

How do you define a simple primary key in a JPA Entity class?

 FAQ

You can define a field as a primary key in an Entity class by annotating the field with javax.persistence.id annotation.

@Entity
public class Order {
 @id
 protected String orderId
}

How do you define a composite primary key in a JPA Entity class?

 FAQ

Composite primary key consists of more than one attribute, which corresponds to a set of single persistent properties or fields.

Composite primary keys must be defined in a primary key class.

Composite primary keys are annotated using the javax.persistence.EmbeddedId and javax.persistence.IdClass annotations.

What are the different kinds of multiplicity relationships between JPA Entity classes?

 FAQ

There are four multiplicity relationships between JPA Entity classes.

1. One-to-one - Each instance of an entity is related to a single instance of another entity. One-to-one relationships use the javax.persistence.OneToOne annotation on the corresponding persistent property or field.

2. One-to-many - Each instance of an entity is related to multiple instances of another entity. One-to-many relationships use the javax.persistence.OneToMany annotation on the corresponding persistent property or field.

3. Many-to-one - Multiple instances of an entity are related to a single instance of another entity. Many-to-one relationships use the javax.persistence.ManyToOne annotation on the corresponding persistent property or field.

4. Many-to-Many - Multiple instances of an entity are related to multiple instances of another entity. Many-to-many relationships use the javax.persistence.ManyToMany annotation on the corresponding persistent property or field.

What are the different kinds of directional relationships between JPA Entity classes?

 FAQ

There are two kinds of directional relationships between JPA entity classes.

Bidirectional - In a bidirectional relation ship, each entity has a field or property that refers to the other entity.

Unidirectional - In a unidirectional entity, only one entity has a field or property that refers to the pother entity.

What are the different kinds of cascade operations applicable for relationships between JPA Entity classes?

 FAQ

Cascade operations defines the dependency of an entity object on the existence of another entity object, where there is a dependency between the two objects.

There are six different types of cascade operations.

1. DETACH - Is parent entity is detached from the persistence context, then the related entity will also be detached.

2. MERGE - If the parent entity is merged into the persistence context, then the related entity will also be merged.

3. PERSIST - If the parent entity is persisted into the persistence context, then the related entity will also be persisted.

4. REFRESH - If the parent entity is refreshed in the persistence context, then the related entity will also be refreshed.

5. REMOVE - If the parent entity is removed from the persistence context then the related entity will also be removed.

6. ALL - All the cascade operations will be applied to the parent entity's related entity.

@OneToMany(cascade=REMOVE)
public List getOrdersLines() { return ordersLines; }

What are JPA Embedded classes?

 FAQ

Embedded classes represent part of the state of an entity, but do not have a persistent identity of their own. Entity classes are annotated with the javax.persistence.Embeddable annotation.

@Entity
public class Person {
 @Id
 protected long id
 protected String fname;
 protected String lname;
 @Embedded
 protected Address address;
 ...
}
 
Subscribe to our Questions

 

Java EE - Interview Questions

Java ServletsJavaServer PagesJavaServer FacesJava API for XML Processing (JAXP)Java API for JSON Processing (JSON-P)Java Message Service API (JMS)Java Persistance API (JPA)Java Transaction API (JTA)JAX-WSJava API for RESTful Web Services (JAX-RS)Java Database Connectivity API (JDBC)Security
 
RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example