HHH-4725 implement orphanRemoval for OneToOne and HHH-4726 Add support for delete-orphan cascading to one-to-one
git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18557 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
8146ab3f9e
commit
b150dc76ae
|
@ -362,6 +362,55 @@ public class BasicHibernateAnnotationsTest extends TestCase {
|
||||||
s.close();
|
s.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testCascadedDeleteOfChildOneToOne() {
|
||||||
|
// create two single player teams (for one versus one match of soccer)
|
||||||
|
// and associate teams with players via the special OneVOne methods.
|
||||||
|
// Clear the Team reference to players, which should orphan the teams.
|
||||||
|
// Orphaning the team should delete the team.
|
||||||
|
|
||||||
|
Session s = openSession();
|
||||||
|
Transaction tx = s.beginTransaction();
|
||||||
|
|
||||||
|
SoccerTeam team = new SoccerTeam();
|
||||||
|
team.setName("Shalrie's team");
|
||||||
|
Player player1 = new Player();
|
||||||
|
player1.setName("Shalrie Joseph");
|
||||||
|
team.setOneVonePlayer(player1);
|
||||||
|
player1.setOneVoneTeam(team);
|
||||||
|
|
||||||
|
s.persist(team);
|
||||||
|
|
||||||
|
SoccerTeam team2 = new SoccerTeam();
|
||||||
|
team2.setName("Taylor's team");
|
||||||
|
Player player2 = new Player();
|
||||||
|
player2.setName("Taylor Twellman");
|
||||||
|
team2.setOneVonePlayer(player2);
|
||||||
|
player2.setOneVoneTeam(team2);
|
||||||
|
s.persist(team2);
|
||||||
|
tx.commit();
|
||||||
|
|
||||||
|
tx = s.beginTransaction();
|
||||||
|
s.clear();
|
||||||
|
team2 = (SoccerTeam)s.load(team2.getClass(), team2.getId());
|
||||||
|
team = (SoccerTeam)s.load(team.getClass(), team.getId());
|
||||||
|
int count = ( (Long) s.createQuery( "select count(*) from Player" ).iterate().next() ).intValue();
|
||||||
|
assertEquals("expected count of 2 but got = " + count, count, 2);
|
||||||
|
|
||||||
|
// clear references to players, this should orphan the players which should
|
||||||
|
// in turn trigger orphanRemoval logic.
|
||||||
|
team.setOneVonePlayer(null);
|
||||||
|
team2.setOneVonePlayer(null);
|
||||||
|
tx.commit();
|
||||||
|
s.close();
|
||||||
|
|
||||||
|
s = openSession();
|
||||||
|
tx = s.beginTransaction();
|
||||||
|
count = ( (Long) s.createQuery( "select count(*) from Player" ).iterate().next() ).intValue();
|
||||||
|
assertEquals("expected count of 0 but got = " + count, count, 0);
|
||||||
|
tx.commit();
|
||||||
|
s.close();
|
||||||
|
}
|
||||||
|
|
||||||
public void testFilter() throws Exception {
|
public void testFilter() throws Exception {
|
||||||
Session s;
|
Session s;
|
||||||
Transaction tx;
|
Transaction tx;
|
||||||
|
|
|
@ -29,6 +29,7 @@ import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.ManyToOne;
|
import javax.persistence.ManyToOne;
|
||||||
|
import javax.persistence.OneToOne;
|
||||||
|
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
|
@ -38,6 +39,10 @@ public class Player {
|
||||||
private String name;
|
private String name;
|
||||||
private SoccerTeam team;
|
private SoccerTeam team;
|
||||||
|
|
||||||
|
// For the world cup of one versus one matches, we have
|
||||||
|
// teams with one player (1v1 team).
|
||||||
|
private SoccerTeam oneVoneTeam;
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
public int getId() {
|
public int getId() {
|
||||||
|
@ -62,4 +67,13 @@ public class Player {
|
||||||
public void setTeam(SoccerTeam team) {
|
public void setTeam(SoccerTeam team) {
|
||||||
this.team = team;
|
this.team = team;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@OneToOne
|
||||||
|
public SoccerTeam getOneVoneTeam() {
|
||||||
|
return oneVoneTeam;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOneVoneTeam(SoccerTeam oneVoneTeam) {
|
||||||
|
this.oneVoneTeam = oneVoneTeam;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -32,17 +32,26 @@ import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.OneToMany;
|
import javax.persistence.OneToMany;
|
||||||
|
import javax.persistence.OneToOne;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class SoccerTeam {
|
public class SoccerTeam {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
|
String name;
|
||||||
|
|
||||||
@OneToMany(mappedBy="team",
|
@OneToMany(mappedBy="team",
|
||||||
orphanRemoval=true,
|
orphanRemoval=true,
|
||||||
cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
|
cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
|
||||||
private Set<Player> players = new HashSet<Player>();
|
private Set<Player> players = new HashSet<Player>();
|
||||||
|
|
||||||
|
@OneToOne(mappedBy="oneVoneTeam",
|
||||||
|
orphanRemoval=true,
|
||||||
|
cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
|
||||||
|
private Player oneVonePlayer;
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
@ -60,4 +69,20 @@ public class SoccerTeam {
|
||||||
val.setTeam(this);
|
val.setTeam(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Player getOneVonePlayer() {
|
||||||
|
return oneVonePlayer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOneVonePlayer(Player oneVonePlayer) {
|
||||||
|
this.oneVonePlayer = oneVonePlayer;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue