summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDevan Goodwin <dgoodwin@redhat.com>2009-12-07 10:29:28 -0400
committerDevan Goodwin <dgoodwin@redhat.com>2009-12-07 10:29:28 -0400
commitb7b8136ab275042b5b69ccb2558a89bd0a15f635 (patch)
treebce3e5b780b0874a944a6738bc759316fd582873
parent318450b755e8a849e035e8d1b8895c1cfb5b278e (diff)
downloadcandlepin-b7b8136ab275042b5b69ccb2558a89bd0a15f635.tar.gz
candlepin-b7b8136ab275042b5b69ccb2558a89bd0a15f635.tar.xz
candlepin-b7b8136ab275042b5b69ccb2558a89bd0a15f635.zip
Change consumer hierarchy to use join column instead of join table.
-rw-r--r--proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java24
-rw-r--r--proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java50
2 files changed, 67 insertions, 7 deletions
diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java
index 1e07c7b..ee4cd56 100644
--- a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java
+++ b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java
@@ -69,12 +69,13 @@ public class Consumer {
@JoinColumn(nullable=false)
private Owner owner;
- // Consumer hierarchy it meant to be useful to represent the relationship between
- // hosts and guests.
- @OneToMany(targetEntity=Consumer.class, cascade=CascadeType.ALL)
- @JoinTable(name="cp_consumer_hierarchy",
- joinColumns=@JoinColumn(name="PARENT_CONSUMER_ID"),
- inverseJoinColumns=@JoinColumn(name="CHILD_CONSUMER_ID"))
+ // Consumers *can* be organized into a hierarchy, could be useful in cases
+ // such as host/guests.
+ @ManyToOne(targetEntity=Consumer.class)
+ @JoinColumn(name="parent_consumer_id")
+ private Consumer parent;
+
+ @OneToMany(mappedBy="parent", cascade=CascadeType.ALL)
private Set<Consumer> childConsumers;
@@ -167,11 +168,20 @@ public class Consumer {
public void setChildConsumers(Set<Consumer> childConsumers) {
this.childConsumers = childConsumers;
}
-
+
public void addChildConsumer(Consumer child) {
+ child.setParent(this);
this.childConsumers.add(child);
}
+ public Consumer getParent() {
+ return parent;
+ }
+
+ public void setParent(Consumer parent) {
+ this.parent = parent;
+ }
+
/**
* @return the consumedProducts
*/
diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java b/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java
index 51399e1..68da901 100644
--- a/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java
+++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java
@@ -187,10 +187,60 @@ public class ConsumerTest extends DatabaseTestFixture {
em.persist(consumer);
commitTransaction();
+ em.clear();
Consumer lookedUp = (Consumer)em.find(Consumer.class, consumer.getId());
assertEquals(2, lookedUp.getChildConsumers().size());
}
+ @Test
+ public void testChildDeleteNoCascade() {
+ beginTransaction();
+
+ Consumer child1 = new Consumer("child1", owner, consumerType);
+ child1.setMetadataField("foo", "bar");
+ consumer.addChildConsumer(child1);
+ em.persist(consumer);
+ commitTransaction();
+
+ em.clear();
+ Long childId = child1.getId();
+ child1 = (Consumer)em.find(Consumer.class, childId);
+ beginTransaction();
+ em.remove(child1);
+ commitTransaction();
+
+ child1 = (Consumer)em.find(Consumer.class, childId);
+ assertNull(child1);
+
+ em.clear();
+ Consumer lookedUp = (Consumer)em.find(Consumer.class, consumer.getId());
+ assertEquals(0, lookedUp.getChildConsumers().size());
+ }
+
+ @Test
+ public void testParentDeleteCascadesToChildren() {
+ beginTransaction();
+
+ Consumer child1 = new Consumer("child1", owner, consumerType);
+ child1.setMetadataField("foo", "bar");
+ consumer.addChildConsumer(child1);
+ em.persist(consumer);
+ commitTransaction();
+
+ Long childId = child1.getId();
+ Long parentId = consumer.getId();
+
+ beginTransaction();
+ em.remove(consumer);
+ commitTransaction();
+
+ em.clear();
+ Consumer lookedUp = (Consumer)em.find(Consumer.class, parentId);
+ assertNull(lookedUp);
+ lookedUp = (Consumer)em.find(Consumer.class, childId);
+ assertNull(lookedUp);
+ }
+
// This this looks like a stupid test but this was actually failing at one point. :)
@Test
public void testMultipleConsumersSameConsumedProduct() {