Implementing the Tweet domain model using JPA annotations will look like the following:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Tweet {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@CreationTimestamp
private Timestamp postTime;
@ManyToOne
private User tweetUser;
@NotNull
private String content;
}
In the preceding code, @Entity is used to mark the Tweet class as a JPA entity. The @Id annotation marks the id property as the identity field of the document. The @Data annotation is from the Lombok library and is used to mark a POJO as a class that will hold data. This means that getters, setters, the equals method, the hashCode method, and the toString method will be generated for that class. @AllArgsConstructor, which will generate a constructor with all the properties, and @NoArgsConstructor, which will generate a default constructor.
Implementing the User domain model using JPA annotations will look like the following code:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class User {
@Id
@NotNull
private String userId;
@JsonIgnore
@NotNull
private String password;
@NotNull
@Column(unique = true)
private String screenName;
@NotNull
private Role role;
private String bio;
private String profileImage;
@ElementCollection
private Set<String> following;
}
In the preceding code, @Entity is used to mark the Tweet class as a JPA entity. @AllArgsConstructor will generate a constructor with all the properties and @NoArgsConstructor will generate a default constructor. @ElementCollection is used to persist a Set collection.
Implementing the Role enum will look like the following code:
public enum Role {
ADMIN, USER
}