Ich habe eine Personen klasse, wobei created_by und updated_by ein self manytoone ist:
Hierbei können "Parent Persons" beliebig viele "Child Persons" erstellen. Wird jetzt "Parent Person" gelöscht, dann sollen alle von dieser Instanz erstellten "Child Persons" auf null gesetzt werden.
Hierzu habe ich eine Test Klasse geschrieben:
Aber irgendwie bekomme ich es nicht hin. Die Test-Funktion stößt immer wieder auf eine AsserationError.
Wie müsste die Entity "Person" korrekt aussehen?
Hierbei möchte ich das, wenn die Parent Person
Code:
@Entity(name="persons")
@Table(indexes = { @Index(name = "IDX_EMAIL_PASSWORD", columnList = "email, password") })
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@CreationTimestamp
private LocalDateTime created_at;
@ManyToOne(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
@JoinColumn(name = "created_by")
private Person created_by;
/*
@OneToMany(fetch = FetchType.LAZY, mappedBy = "createdBy")
@JoinColumn(name = "created_by")
private List<Person> created;
*/
@Null
@UpdateTimestamp
private LocalDateTime updated_at;
@ManyToOne(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
@JoinColumn(name = "updated_by")
private Person updated_by;
/*
@OneToMany(fetch = FetchType.LAZY, mappedBy = "updatedBy")
@JoinColumn(name = "updated_by")
private List<Person> updated;
*/
@javax.validation.constraints.Pattern(regexp = "(?=.*\\S.*.*).{3,255}")
private String firstname;
@javax.validation.constraints.Pattern(regexp = "(?=.*\\S.*.*).{3,255}")
private String lastname;
@Column(unique=true)
@Email(message = "Email should be valid")
private String email;
@javax.validation.constraints.Pattern(regexp = "(?=.*\\S.*.*).{6,255}")
private String password;
public Person() {
}
public Person(Person person) {
super();
this.firstname = person.firstname;
this.lastname = person.lastname;
this.email = person.email;
this.password = person.password;
}
public Person(@Pattern(regexp = "(?=.*\\S.*.*).{3,255}") String firstname,
@Pattern(regexp = "(?=.*\\S.*.*).{3,255}") String lastname,
@Email(message = "Email should be valid") String email,
@Pattern(regexp = "(?=.*\\S.*.*).{6,255}") String password) {
super();
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.password = password;
}
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 Person getCreatedBy() {
return created_by;
}
public void setCreatedBy(Person created_by) {
this.created_by = created_by;
}
public LocalDateTime getUpdatedAt() {
return updated_at;
}
public void setUpdatedAt(LocalDateTime updated_at) {
this.updated_at = updated_at;
}
public Person getUpdatedBy() {
return updated_by;
}
public void setUpdatedBy(Person updated_by) {
this.updated_by = updated_by;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
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 int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
public Person clone() {
return new Person(this);
}
@Override
public String toString() {
return "Person [id=" + id + ", created_at=" + created_at + ", created_by=" + created_by + ", updated_at="
+ updated_at + ", updated_by=" + updated_by + ", firstname=" + firstname + ", lastname=" + lastname
+ ", email=" + email + ", password=" + password + "]";
}
}
Hierbei können "Parent Persons" beliebig viele "Child Persons" erstellen. Wird jetzt "Parent Person" gelöscht, dann sollen alle von dieser Instanz erstellten "Child Persons" auf null gesetzt werden.
Hierzu habe ich eine Test Klasse geschrieben:
Code:
@Test
public void testDeleteChildPersonForCreatedBy() {
// initialize
String firstnameValue1 = "firstname1";
String lastnameValue1 = "lastname1";
String emailValue1 = "email1@email.de";
String passwordValue1 = "password1";
Person parentPerson = new Person();
parentPerson.setFirstname(firstnameValue1);
parentPerson.setLastname(lastnameValue1);
parentPerson.setEmail(emailValue1);
parentPerson.setPassword(passwordValue1);
personRepository.save(parentPerson);
assertTrue(parentPerson.getId() > 0);
String firstnameValue2 = "firstname2";
String lastnameValue2 = "lastname2";
String emailValue2 = "email2@email.de";
String passwordValue2 = "password2";
Person childPerson = new Person();
childPerson.setCreatedBy(parentPerson);
childPerson.setFirstname(firstnameValue2);
childPerson.setLastname(lastnameValue2);
childPerson.setEmail(emailValue2);
childPerson.setPassword(passwordValue2);
personRepository.save(childPerson);
assertTrue(childPerson.getId() > 0);
// test
personRepository.delete(childPerson);
long counts = personRepository.count();
assertEquals(1L, counts);
// clean up
personRepository.delete(parentPerson);
}
Aber irgendwie bekomme ich es nicht hin. Die Test-Funktion stößt immer wieder auf eine AsserationError.
Wie müsste die Entity "Person" korrekt aussehen?
Hierbei möchte ich das, wenn die Parent Person