Search Tutorials


Top Java Persistence API(JPA) Interview Questions | JavaInUse



Top Java Persistence API(JPA) frequently asked interview questions.

In this post we will look at Java Persistence API(JPA) Interview questions. Examples are provided with explanations.


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
Q: JPA EntityManager: Why use persist() over merge()?
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.


Q: Why does JPA have a @Transient annotation?
Example - String test = "This is a test String and 'This is data we want'"
A:
Java's transient keyword is used to denote that a field is not to be serialized, whereas JPA's @Transient annotation is used to indicate that a field is not to be persisted in the database, i.e. their semantics are different. Is it possible to set a default value for columns in JPA, and if, how is it done using annotations? Actually it is possible in JPA, although a little bit of a hack using the columnDefinition property of the @Column annotation, for example: @Column(name="Price", columnDefinition="Decimal(10,2) default '100.00'")

Q: In which case do you use JPA @JoinTable annotation?
A:
It's the only solution to map a ManyToMany association : you need a join table between the to entities tables to map the association. It's also used for OneToMany (usually unidirectional) associations, when you don't want to add a foreign key in the table of the many side, and thus keep it independant of the one side. Search for @JoinTable in the hibernate documentation for explanations and examples.

Q: What is the difference between @Column and @Basic annotations in JPA?
A:
@Basic signifies that an attribute is to be persisted and a standard mapping is to be used. It has parameters which allow you to specify whether the attribute is to be lazily loaded and whether it's nullable. @Column allows you to specify the name of the column in the database to which the attribute is to be persisted.

See Also

Spring Boot Interview Questions Apache Camel Interview Questions Drools Interview Questions Java 8 Interview Questions Enterprise Service Bus- ESB Interview Questions. JBoss Fuse Interview Questions Angular 2 Interview Questions