×
☰ See All Chapters

What is JPA entity class - Defining a JPA Entity Class

Entity classes are also called as persistence classes. Classes whose objects are used to persist (save) the data into data base are called as persistence class.  Persistence class or an entity class is a POJO (Plain Old Java Object) class, i.e. an ordinary Java class that is annotated with @Entity annotation.

JPA recognizes two types of persistent classes: entity classes and embeddable classes. A class annotated with @Entity annotation will have a distinct separate existence. We can run DB queries, without being dependent on any other class. Each persistent instance of an entity class - each entity - represents a unique database record. You can use the EntityManager to find an entity by its persistent identity (persistent identifier is covered later in this chapter) or use a Query to find entities matching certain criteria.

A class annotated with @Embeddable annotation will not have independent existence. We cannot run DB queries, without depending on other class. An instance of an embeddable class, on the other hand, is only stored as part of a separate entity. Embeddable instances have no persistent identity, and are never returned directly from the EntityManager or from a Query unless the query uses a projection on owning class to the embedded instance. For example, if Address is embedded in User, then a query "SELECT a FROM Address a" will never return the embedded Address of User; but a projection query such as "SELECT u.address FROM User u" will.

Typically, embeddable classes share the same table as the Entity in which they are embedded.

Restrictions on Persistent Classes/Entity classes

  1. It should be POJO class.  

  2. Try to have properties/private variable names same as that in Database column names.  

  3. Try to have same name for this class as that in Database table name.  

  4. A no-arg constructor: It is recommended that you have a default constructor at least package visibility so that JPA can create the instance of the Persistent class by newInstance() method.   

  5. Entity classes may not be final. No method of an entity class can be final. JPA does not support static or final fields.  

  6. JPA uses a version field in entities to detect concurrent modifications to the same database record. When the JPA runtime detects an attempt to concurrently modify the same record, it throws an exception to the last transaction attempting to commit. It prevents overwriting the previous commit with stale data. A version field is usually not required, but when there are concurrent threads or processes on same record at the same time, then it results into unexpected changes. This is unacceptable to most applications. 

Entity class and inheritance

Entity class can involve in java inheritance. Persistent classes can inherit from non-persistent classes, other persistent classes, and non-persistent classes can inherit from persistent classes. There are, however, a few important limitations:

    1. Persistent classes should not inherit from natively-implemented system classes like java.net.Socket and java.lang.Thread. 

    2. If a persistent class inherits from a non-persistent class, the fields of the non-persistent superclass cannot be persisted. 

    3. All classes in an inheritance tree must use the same identity type. 

Data type of the entity classes

Data type of the entity classes can be immutable types, mutable types, and relations.

Immutable types

Mutable types

The values of fields with immutable fields cannot be modified once created.  The only way to alter a persistent field of an immutable type is to assign a new value to the field.

The values of Persistent fields of mutable types can be altered without assigning the field a new value. Values can be modified directly through their own methods.

JPA supports the following immutable types:

  1. All primitives  

  2. All primitive wrappers  

  3. java.lang.String 

  4. java.math.BigInteger 

  5. java.math.BigDecimal 

  6. Arrays of above types. But you should not manipulate individual array indexes without resetting the array into the persistent field. 

JPA supports the following mutable types:

  1. java.util.Date 

  2. java.util.Calendar 

  3. java.sql.Date 

  4. java.sql.Timestamp 

  5. java.sql.Time 

  6. Enums 

  7. Entity types (relations/Mapping between entities) 

  8. Embeddable types 

  9. java.util.Collection of entity classes 

  10. java.util.Set of entity classes 

  11. java.util.List of entity classes 

  12. java.util.Map in which each entry is a mapping between the value of one of a related entity's fields and that entity. 

 


All Chapters
Author