Entities

Let's define a couple of entities to use in our example. We will create an entity Todo to manage todos. A simple example is shown as follows:

   @Entity   public class Todo {     @Id     @GeneratedValue(strategy = GenerationType.AUTO)     private Long id;     @ManyToOne(fetch = FetchType.LAZY)     @JoinColumn(name = "userid")     private User user;     private String title;     private String description;     private Date targetDate;     private boolean isDone;     public Todo() {// Make JPA Happy    }   }

Important things to note are as follows:

  • Todo has a title, a description, a target date, and a completion indicator (isDone). JPA needs a constructor.
  • @Entity: The annotation specifies that the class is an entity.
  • @Id: Specifies that ID is the primary key of the entity.
  • @GeneratedValue(strategy ...

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.