Java EE 7: JPA Entity Types

JPA Entity Types

Question

Which of these can be a JPA entity?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

A.

In Java Persistence API (JPA), an entity is a lightweight Java class that represents an object or a set of objects in a relational database. The entity class must follow certain rules and guidelines defined by JPA. These rules include:

  • The entity class must have a no-argument constructor (either explicitly defined or implicitly provided by the compiler).
  • The entity class must have a primary key (either a single attribute or a composite key).
  • The entity class must have getter and setter methods for its attributes.
  • The entity class must not be final or abstract.

Given these rules, we can eliminate options B and C, since an abstract class or an interface cannot be instantiated and therefore cannot be used as an entity.

Option A, an Enum type, can be used as a JPA entity. Enum types are a special kind of Java class that represent a fixed set of values. Enum types are typically used to represent things like days of the week, months of the year, or status codes. In JPA, an Enum type can be mapped to a database column using the @Enumerated annotation.

Option D, a final class, cannot be used as a JPA entity. A final class cannot be extended by other classes, which means that JPA cannot create a proxy object for it to implement lazy loading and other optimizations.

Therefore, the correct answer is A.