Sitiuation: trying to remove a person that has a collection of persons(friends). Ofcourse i don't want the friends to be delete so i have to be carefull with that.
code for Person:
@Entity
public class Person {
private String naam, voornaam, email, password;
@Id
private String username;
private String status;
@ManyToMany
private Collection<Person> vrienden;
@OneToMany(orphanRemoval = true)
private Collection<Post> posts;
@Enumerated(EnumType.STRING)
private Role role;
public Person(String naam, String voornaam, String email, String password, String username) {
setNaam(naam);
setEmail(email);
setStatus("online");
setVoornaam(voornaam);
setPassword(password);
setUsername(username);
vrienden = new HashSet<>();
posts = new HashSet<>();
role = Role.USER;
}
public static void addFriend(Person a, Person b) {
a.addFriend(b);
b.addFriend(a);
}
private void addFriend(Person b) {
this.vrienden.add(b);
}
public static void deleteFriend(Person a, Person b) {
a.deleteFriend(b);
b.deleteFriend(a);
}
private void deleteFriend(Person a) {
this.vrienden.remove(a);
}
public Collection<Person> getFriends() {
return vrienden;
}
public Role getRole() {
return this.role;
}
public void setRole(Role role) {
this.role = role;
}
@NotNull(message = "{error.no.name}")
@Size(min = 2, message = "{error.invalid.namesize}")
public String getNaam() {
return naam;
}
public void setNaam(String naam) {
this.naam = naam;
}
@NotNull(message = "{error.no.surnaam}")
@Size(min = 2, message = "{error.invalid.surnamesize}")
public String getVoornaam() {
return voornaam;
}
public void setVoornaam(String voornaam) {
this.voornaam = voornaam;
}
@NotNull(message = "{error.no.email}")
@Email(message = "{error.invalid.email}")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@NotNull(message = "{error.no.status}")
@Size(min = 1, message = "{error.no.valid.status}")
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@NotNull(message = "{error.no.username}")
@Size(min = 2, message = "{error.invalid.usernamesize}")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = Password.getSaltedHash(password);
}
public boolean isPasswordCorrect(String password) {
boolean result = false;
result = Password.check(password, this.password);
return result;
}
@NotNull(message = "{error.no.password}")
@Size(min = 2, message = "{error.invalid.usernamesize}")
public String getPassword() {
return this.password;
}
public void addPost(Post p) {
if (p == null) {
throw new DomainException("Post is null");
}
posts.add(p);
}
public void deletePost(Post p) {
if (p == null) {
throw new DomainException("Post is null");
}
posts.remove(p);
}
public Collection<Post> getPosts() {
return posts;
}
@Override
public int hashCode() {
int hash = 3;
hash = 59 * hash + Objects.hashCode(this.username);
return hash;
}
public void setHashedPassword(String password) {
this.password = password;
}
}
The method that i'm currently using is:
public void DeletePerson(String user) {
entityManager.getTransaction().begin();
entityManager.createQuery("delete from Person p where p.username=:username").setParameter("username", user).executeUpdate();
entityManager.getTransaction().commit();
}
It gives the following error:
java.sql.SQLIntegrityConstraintViolationException: DELETE on table 'PERSON' caused a violation of foreign key constraint 'PRSNPRSNVRNDNSRNME' for key (vincent). The statement has been rolled back.
My guess is to add something to the manytomany annotation
create tables: https://i.stack.imgur.com/zS2Xf.png
UPDATE:
The code that i use now for deleting succesfully:
public void DeletePerson(String user) {
entityManager.getTransaction().begin();
entityManager.createNativeQuery("delete from PERSON_PERSON p where p.PERSON_USERNAME=?username or p.VRIENDEN_USERNAME=?username").setParameter("username", user).executeUpdate();
entityManager.flush();
Person tempPerson = getPerson(user);
entityManager.remove(tempPerson);
entityManager.getTransaction().commit();
}
but now i still retrieve old references although the data is deleted in the tables. I retrieve that data with:
entityManager.find(Person.class, user);