JPA Entities
Java beans can be mapped to the database tables.
This can be done with XML or annotations.
Constraints
Usage of JPA annotations or XML mapping
Non final classes
Non final getters and setters
Additional Constraints
These are recommended, but not compulsory:
Extend the equals method
Extend the hashCode method
Extend the toString method
Comparison Methods
For the equals and hashCode methods do not use the id for this, make use of the unique fields.
Annotations
Ids
Id columns are annotated with @Id:
When using composite ids these can be defined into a class id:
This way they can be handled as a single object.
Defining the Entity Name
By default JPA implementations will take the name of the class as the entity name. In this case the entity will be named SomeEntity.
This can be changed by defining a name in the annotation. This will set the entity name as NamedEntity:
Transient Fields
Any field containing data which should not be persisted has to be marked with the @Transient field. It is not recommended using this annotation, and it may be a hint telling that the entity is doing something else apart from storing persistent data.
XML
Additional Constraints
Requirementes may vary between implementations, but there are some recommended thinks to take care of:
Avoid final classes
Avoid final getters and setters for lazy fields
Last updated
Was this helpful?