-
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
-
@Id
-
@Column
-
@Temporal
- @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.
-
@Data: A convenient shortcut annotation that bundles the features of @ToString, @EqualsAndHashCode, @Getter / @Setter and @RequiredArgsConstructor together.
@Entity
@Table(name = "documents")
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Data
public class Document {
@Id
@Column(name = "code")
private String code;
@Column(name = "source")
private String source;
@Column(name = "code_list_code")
private String codeListCode;
@Column(name = "display_value")
private String displayValue;
@Column(name = "long_description")
private String longDescription;
@Column(name = "from_date")
@DateTimeFormat(pattern = "dd-MM-yyyy")
@Temporal(TemporalType.DATE)
private Date fromDate;
@Column(name = "to_date")
@DateTimeFormat(pattern = "dd-MM-yyyy")
@Temporal(TemporalType.DATE)
private Date toDate;
@Column(name = "sorting_priority")
private Integer sortingPriority;
}