Author: smarlow(a)redhat.com
Date: 2009-12-22 13:07:38 -0500 (Tue, 22 Dec 2009)
New Revision: 18318
Added:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/Player.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/SoccerTeam.java
Modified:
core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/BasicHibernateAnnotationsTest.java
Log:
HHH-4601 implement orphanRemoval for OneToMany. OneToOne is not yet done (see HHH-4725)
Modified: core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java
===================================================================
---
core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java 2009-12-22
15:08:07 UTC (rev 18317)
+++
core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java 2009-12-22
18:07:38 UTC (rev 18318)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
@@ -1486,7 +1486,7 @@
}
}
bindManyToOne(
- getCascadeStrategy( ann.cascade(), hibernateCascade ),
+ getCascadeStrategy( ann.cascade(), hibernateCascade, false),
joinColumns,
ann.optional(),
ignoreNotFound, onDeleteCascade,
@@ -1521,7 +1521,7 @@
}
}
bindOneToOne(
- getCascadeStrategy( ann.cascade(), hibernateCascade ),
+ getCascadeStrategy( ann.cascade(), hibernateCascade, false),
joinColumns,
ann.optional(),
getFetchMode( ann.fetch() ),
@@ -1550,7 +1550,7 @@
joinColumn.setSecondaryTableName( join.getTable().getName() );
}
}
- bindAny( getCascadeStrategy( null, hibernateCascade ), //@Any has not cascade
attribute
+ bindAny( getCascadeStrategy( null, hibernateCascade, false), //@Any has not cascade
attribute
joinColumns, onDeleteCascade, nullability,
propertyHolder, inferredData, entityBinder,
isIdentifierMapper, mappings );
@@ -1756,7 +1756,8 @@
collectionBinder.setTargetEntity(
mappings.getReflectionManager().toXClass( oneToManyAnn.targetEntity() )
);
- collectionBinder.setCascadeStrategy( getCascadeStrategy( oneToManyAnn.cascade(),
hibernateCascade ) );
+ collectionBinder.setCascadeStrategy(
+ getCascadeStrategy( oneToManyAnn.cascade(), hibernateCascade,
oneToManyAnn.orphanRemoval()) );
collectionBinder.setOneToMany( true );
}
else if ( elementCollectionAnn != null
@@ -1783,7 +1784,7 @@
collectionBinder.setTargetEntity(
mappings.getReflectionManager().toXClass( manyToManyAnn.targetEntity() )
);
- collectionBinder.setCascadeStrategy( getCascadeStrategy( manyToManyAnn.cascade(),
hibernateCascade ) );
+ collectionBinder.setCascadeStrategy( getCascadeStrategy( manyToManyAnn.cascade(),
hibernateCascade, false) );
collectionBinder.setOneToMany( false );
}
else if ( property.isAnnotationPresent( ManyToAny.class ) ) {
@@ -1791,7 +1792,7 @@
collectionBinder.setTargetEntity(
mappings.getReflectionManager().toXClass( void.class )
);
- collectionBinder.setCascadeStrategy( getCascadeStrategy( null, hibernateCascade ) );
+ collectionBinder.setCascadeStrategy( getCascadeStrategy( null, hibernateCascade,
false) );
collectionBinder.setOneToMany( false );
}
collectionBinder.setMappedBy( mappedBy );
@@ -2464,8 +2465,8 @@
}
private static String getCascadeStrategy(
- javax.persistence.CascadeType[] ejbCascades, Cascade hibernateCascadeAnnotation
- ) {
+ javax.persistence.CascadeType[] ejbCascades, Cascade hibernateCascadeAnnotation,
+ boolean orphanRemoval) {
EnumSet<CascadeType> hibernateCascadeSet = convertToHibernateCascadeType(
ejbCascades );
CascadeType[] hibernateCascades = hibernateCascadeAnnotation == null ?
null :
@@ -2475,6 +2476,11 @@
hibernateCascadeSet.addAll( Arrays.asList( hibernateCascades ) );
}
+ if ( orphanRemoval ) {
+ hibernateCascadeSet.add(CascadeType.DELETE_ORPHAN);
+ hibernateCascadeSet.add(CascadeType.REMOVE);
+ }
+
StringBuilder cascade = new StringBuilder();
for ( CascadeType aHibernateCascadeSet : hibernateCascadeSet ) {
switch ( aHibernateCascadeSet ) {
Modified:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/BasicHibernateAnnotationsTest.java
===================================================================
---
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/BasicHibernateAnnotationsTest.java 2009-12-22
15:08:07 UTC (rev 18317)
+++
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/BasicHibernateAnnotationsTest.java 2009-12-22
18:07:38 UTC (rev 18318)
@@ -309,8 +309,56 @@
s.close();
}
-
+ public void testCascadedDeleteOfChildEntitiesBug2() {
+ // Relationship is one SoccerTeam to many Players.
+ // Create a SoccerTeam (parent) and three Players (child).
+ // Verify that the count of Players is correct.
+ // Clear the SoccerTeam reference Players.
+ // The orphanRemoval should remove the Players automatically.
+ // @OneToMany(mappedBy="name", orphanRemoval=true)
+ Session s = openSession();
+ Transaction tx = s.beginTransaction();
+
+ SoccerTeam team = new SoccerTeam();
+ int teamid = team.getId();
+ Player player1 = new Player();
+ player1.setName("Shalrie Joseph");
+ team.addPlayer(player1);
+
+ Player player2 = new Player();
+ player2.setName("Taylor Twellman");
+ team.addPlayer(player2);
+
+ Player player3 = new Player();
+ player3.setName("Steve Ralston");
+ team.addPlayer(player3);
+ s.persist(team);
+ tx.commit();
+ s.close();
+
+ s = openSession();
+ tx = s.beginTransaction();
+ team = (SoccerTeam)s.merge(team);
+ int count = ( (Long) s.createQuery( "select count(*) from Player"
).iterate().next() ).intValue();
+ assertEquals("expected count of 3 but got = " + count, count, 3);
+
+ // clear references to players, this should orphan the players which should
+ // in turn trigger orphanRemoval logic.
+ team.getPlayers().clear();
+// 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();
+
+ 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 {
Session s;
Transaction tx;
@@ -581,7 +629,9 @@
Topic.class,
Narrative.class,
Drill.class,
- PowerDrill.class
+ PowerDrill.class,
+ SoccerTeam.class,
+ Player.class
};
}
Added:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/Player.java
===================================================================
---
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/Player.java
(rev 0)
+++
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/Player.java 2009-12-22
18:07:38 UTC (rev 18318)
@@ -0,0 +1,65 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors
as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat, Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+
+package org.hibernate.test.annotations.entity;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+
+
+@Entity
+public class Player {
+
+ private int id;
+ private String name;
+ private SoccerTeam team;
+
+ @Id
+ @GeneratedValue
+ public int getId() {
+ return id;
+ }
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ @Column(name="name")
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ @ManyToOne
+ public SoccerTeam getTeam() {
+ return team;
+ }
+ public void setTeam(SoccerTeam team) {
+ this.team = team;
+ }
+}
\ No newline at end of file
Added:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/SoccerTeam.java
===================================================================
---
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/SoccerTeam.java
(rev 0)
+++
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/SoccerTeam.java 2009-12-22
18:07:38 UTC (rev 18318)
@@ -0,0 +1,63 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors
as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat, Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+
+package org.hibernate.test.annotations.entity;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+
+@Entity
+public class SoccerTeam {
+ @Id
+ @GeneratedValue
+ private int id;
+ @OneToMany(mappedBy="team",
+ orphanRemoval=true,
+ cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH,
CascadeType.DETACH})
+ private Set<Player> players = new HashSet<Player>();
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public Set<Player> getPlayers() {
+ return players;
+ }
+
+ public void addPlayer(Player val) {
+ players.add(val);
+ val.setTeam(this);
+ }
+
+}
\ No newline at end of file