I’m trying to set up a test in Hibernate for setting a many to many relation to null to investigate a related bug I’m dealing with, and I want to rule out the HQL as the issue.
Here is my test model:
@Entity(name = "person")
public static class Person {
@Id
@Column(name = "person_id")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "team_id")
private Team team;
public Person() {
}
public Person(Long id) {
this.id = id;
}
public Team getTeam() {
return team;
}
public void setTeam(Team team) {
this.team = team;
}
}
@Entity(name = "team")
public static class Team {
@Id
private Long id;
public Team() {}
public Team(Long id) {
this.id = id;
}
}
And my test query goes as follows:
Team team = new Team( 1L );
session.persist( team );
Person person = new Person( 1L );
person.setTeam( team );
session.persist( person );
person = session.find( Person.class, 1L );
assertNotNull( person.getTeam() );
session.createMutationQuery( "update person p set p.team = null" ).executeUpdate();
session.flush();
person = session.find( Person.class, 1L );
assertNull( person.getTeam() );
But in the final assertion it fails since the team is still pointing to the referenced team rather than null. Is anyone able to take a quick look at this and point out if I’m making any mistakes in my code?