In einem anderen Projekt habe ich für meinen Benutzer eine self-reference auf created_by, updated_by und deleted_by
Wie muss ich es in Spring MongoDB korrekt schreiben? Mit @DBRef?
Java:
@Entity
@Table(name = "user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
/**
* the id of the Language
*
* @var PrimaryKey
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/**
* the create date of the user
*
* @var TIMESTAMP
*/
@CreationTimestamp
private LocalDateTime created_at;
/**
* the user who create the user
*
* @var INTEGER(11)
*/
@JoinColumn(name = "created_by", referencedColumnName = "id")
@ManyToOne(fetch = FetchType.LAZY)
private User created_by;
@OneToMany(mappedBy = "created_by", fetch = FetchType.LAZY)
private List<User> createdUsers;
/**
* the last update of the user
*
* @var TIMESTAMP
*/
@UpdateTimestamp
private LocalDateTime updated_at;
/**
* the user who update the entity as last
*
* @var INTEGER(11)
*/
@JoinColumn(name = "updated_by", referencedColumnName = "id")
@ManyToOne(fetch = FetchType.LAZY)
private User updated_by;
@OneToMany(mappedBy = "updated_by", fetch = FetchType.LAZY)
private List<User> updatedUsers;
/**
* the firstname of the user
*
* @var String[255] not null
*/
@NotBlank
@Length(min = 3, max = 255)
private String firstname;
/**
* the lastname of the user
*
* @var String[255] not null
*/
@NotBlank
@Length(min = 3, max = 255)
private String lastname;
/**
* the email of the user
*
* @var String[255] not null
*/
@NotBlank
@Length(min = 5, max = 255)
@Email(message = "Email should be valid")
private String email;
/**
* the password of the user
*
* @var String[255] not null
*/
@NotBlank
@Length(max = 255)
private String password;
public User() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public LocalDateTime getCreatedAt() {
return created_at;
}
public void setCreatedAt(LocalDateTime created_at) {
this.created_at = created_at;
}
public LocalDateTime getUpdatedAt() {
return updated_at;
}
public void setUpdatedAt(LocalDateTime updated_at) {
this.updated_at = updated_at;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public User getCreatedBy() {
return created_by;
}
public void setCreatedBy(User created_by) {
this.created_by = created_by;
}
public List<User> getCreatedUsers() {
return createdUsers;
}
public void setCreatedUsers(List<User> userList) {
this.createdUsers = userList;
}
public User getUpdatedBy() {
return updated_by;
}
public List<User> getUpdatedUsers() {
return updatedUsers;
}
public void setUpdatedUsers(List<User> updatedUsers) {
this.updatedUsers = updatedUsers;
}
public void setUpdatedBy(User updated_by) {
this.updated_by = updated_by;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", created_at=" + created_at + ", created_by=" + created_by + ", createdUsers="
+ createdUsers + ", updated_at=" + updated_at + ", updated_by=" + updated_by + ", updatedUsers="
+ updatedUsers + ", firstname=" + firstname + ", lastname=" + lastname + ", email=" + email
+ ", password=" + password + "]";
}
}
Wie muss ich es in Spring MongoDB korrekt schreiben? Mit @DBRef?