-
Notifications
You must be signed in to change notification settings - Fork 9
JPA
At this moment we have only this JPA entity model to persist on H2 database memory. Here is the definition of this POJO, and lets explain each of the annotations used here.
-
@Entity: Indicate that it is a JPA entity and should be mapped to a JPA database table. We must ensure a non-arguments constructor and primary key on POJO class.
-
@Table: This annotation specifies the primary table for the annotated entity. Let you specify the details of the table that will be used to persist the entity in the database. Provides four attributes, allowing you to override the name of the table, catalog, schema and enforce unique constraints on columns in the table. If no Table annotation is specified for an entity class, the default values apply.
-
@Id: Specifies the primary key of an entity. The field or property to which the Id annotation is applied should be one of the following types: any Java primitive type; any primitive wrapper type; String; java.util.Date; java.sql.Date; java.math.BigDecimal; java.math.BigInteger. The mapped column for the primary key of the entity is assumed to be the primary key of the primary table. If no Column annotation is specified, the primary key column name is assumed to be the name of the primary key property or field.
-
@Column: Is used to specify the mapped column for a persistent property or field. If no Column annotation is specified, the default values apply.
-
@Temporal: This annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar. It may only be specified for fields or properties of these types. In JPA, @Temporal annotation solves the one of the major issue of converting the date and time values from Java object to compatible database type and retrieving back to the application. The mapping between the Java 8 Date/Time classes and the SQL types is implicit, there is not need to specify the @Temporal annotation.
- @DateTimeFormat: Declares that a field or method parameter should be formatted as a date or time. Supports formatting by style pattern, ISO date time pattern, or custom format pattern string. Can be applied to Date, Calendar, Long.
I have to say that when it comes to writing Java code, Lombok is one of the best libraries you can have.
Helps you to avoid all that boilerplate code with getters, setters, and constructors code on POJOs classes making your code more legible.
Dependency configuration is declared on pom.xml file as follow:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
-
@NoArgsConstructor: Will generate a constructor with no parameters. If this is not possible (because of final fields), a compiler error will result instead.
-
@AllArgsConstructor: Generates a constructor with one parameter for each field in your class. Fields marked with @NonNull result in null checks on those parameters.
-
@Builder: This annotation produces complex builder APIs for our class. Will allows you create class instances with the Builder pattern as follows:
Document doc = Document.builder()
.code("CODE1")
.codeListCode("ZBA00001")
.displayValue("Display value")
.longDescription("Description")
.build();
- @Data: A convenient shortcut annotation that bundles the features of @ToString, @EqualsAndHashCode, @Getter / @Setter and @RequiredArgsConstructor together.
There are the entitiy POJO classes used to persist on database.
In order to access our data stored in our database we need to declare our interface that extends JpaRepository <TEntity, TKey>
, where TEntity is the POCO class that represents our data and decorated with the @Entity annotation and TKey is the data type of our primary key. This class must use the @Repository annotation.
The is the DocumentRepository repository interface to access the Document database table.
@Repository
public interface DocumentRepository extends JpaRepository<Document, String> {
}