From 3a30375aabcfc58a2ca83381caec4079f2249c7e Mon Sep 17 00:00:00 2001 From: Devan Goodwin Date: Thu, 3 Dec 2009 15:39:37 -0400 Subject: Make owner -> consumer bi-directional relationship. Though you aparently still have to add the object references on both sides. :( --- .../fedoraproject/candlepin/model/Consumer.java | 2 +- .../org/fedoraproject/candlepin/model/Owner.java | 38 ++++++++++++---------- .../candlepin/model/test/OwnerTest.java | 28 ++++++++++++++-- .../org/fedoraproject/candlepin/test/TestUtil.java | 7 ++-- 4 files changed, 52 insertions(+), 23 deletions(-) diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java index 6bc0019..1e20d3e 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java @@ -66,7 +66,7 @@ public class Consumer { @ManyToOne @ForeignKey(name="fk_consumer_owner") - @JoinColumn(nullable=false) + @JoinColumn(nullable=false, name="owner_id", referencedColumnName="id") private Owner owner; // Consumer hierarchy it meant to be useful to represent the relationship between diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Owner.java b/proxy/code/src/org/fedoraproject/candlepin/model/Owner.java index 4765410..61f0787 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Owner.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Owner.java @@ -14,8 +14,8 @@ */ package org.fedoraproject.candlepin.model; -import java.util.LinkedList; -import java.util.List; +import java.util.HashSet; +import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; @@ -53,21 +53,24 @@ public class Owner { // TODO: Remove these transients once the appropriate objects are mapped: - @Transient - private List consumers; + @OneToMany(mappedBy="owner", targetEntity=Consumer.class) + private Set consumers; // EntitlementPool is the owning side of this relationship. @OneToMany(mappedBy="owner", targetEntity=EntitlementPool.class) @ForeignKey(name="fk_user_owner_id") - private List entitlementPools; + private Set entitlementPools; @Transient - private List users; + private Set users; /** * Default constructor. */ public Owner() { + consumers = new HashSet(); + entitlementPools = new HashSet(); + users = new HashSet(); } /** @@ -77,6 +80,10 @@ public class Owner { */ public Owner(String nameIn) { this.name = nameIn; + + consumers = new HashSet(); + entitlementPools = new HashSet(); + users = new HashSet(); } /** @@ -110,37 +117,37 @@ public class Owner { /** * @return the consumers */ - public List getConsumers() { + public Set getConsumers() { return consumers; } /** * @param consumers the consumers to set */ - public void setConsumers(List consumers) { + public void setConsumers(Set consumers) { this.consumers = consumers; } /** * @return the entitlementPools */ - public List getEntitlementPools() { + public Set getEntitlementPools() { return entitlementPools; } /** * @param entitlementPools the entitlementPools to set */ - public void setEntitlementPools(List entitlementPools) { + public void setEntitlementPools(Set entitlementPools) { this.entitlementPools = entitlementPools; } /** * @return the users */ - public List getUsers() { + public Set getUsers() { return users; } /** * @param users the users to set */ - public void setUsers(List users) { + public void setUsers(Set users) { this.users = users; } @@ -151,7 +158,7 @@ public class Owner { public void addUser(User u) { u.setOwner(this); if (this.users == null) { - this.users = new LinkedList(); + this.users = new HashSet(); } this.users.add(u); } @@ -162,9 +169,6 @@ public class Owner { */ public void addConsumer(Consumer c) { c.setOwner(this); - if (this.consumers == null) { - this.consumers = new LinkedList(); - } this.consumers.add(c); } @@ -176,7 +180,7 @@ public class Owner { public void addEntitlementPool(EntitlementPool pool) { pool.setOwner(this); if (this.entitlementPools == null) { - this.entitlementPools = new LinkedList(); + this.entitlementPools = new HashSet(); } this.entitlementPools.add(pool); } diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/test/OwnerTest.java b/proxy/code/src/org/fedoraproject/candlepin/model/test/OwnerTest.java index bbef5db..c42af0a 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/OwnerTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/OwnerTest.java @@ -15,11 +15,13 @@ package org.fedoraproject.candlepin.model.test; import org.fedoraproject.candlepin.model.Consumer; +import org.fedoraproject.candlepin.model.ConsumerType; import org.fedoraproject.candlepin.model.EntitlementPool; import org.fedoraproject.candlepin.model.Owner; import org.fedoraproject.candlepin.model.Product; import org.fedoraproject.candlepin.model.User; import org.fedoraproject.candlepin.test.DatabaseTestFixture; +import org.fedoraproject.candlepin.test.TestUtil; import java.util.List; @@ -38,9 +40,6 @@ public class OwnerTest extends DatabaseTestFixture { .setParameter("name", ownerName).getSingleResult(); assertNotNull(result); assertEquals(ownerName, result.getName()); -// assertEquals(0, result.getConsumers().size()); -// assertEquals(0, result.getEntitlementPools().size()); -// assertEquals(0, result.getUsers().size()); assertTrue(result.getId() > 0); assertEquals(o.getId(), result.getId()); } @@ -93,4 +92,27 @@ public class OwnerTest extends DatabaseTestFixture { assertEquals(1, owner.getEntitlementPools().size()); } + + @Test + public void bidirectionalConsumers() throws Exception { + beginTransaction(); + Owner o = TestUtil.createOwner(); + ConsumerType consumerType = TestUtil.createConsumerType(); + Consumer c1 = TestUtil.createConsumer(consumerType, o); + Consumer c2 = TestUtil.createConsumer(consumerType, o); + o.addConsumer(c1); + o.addConsumer(c2); + em.persist(o); + em.persist(consumerType); + em.persist(c1); + em.persist(c2); + + commitTransaction(); + + assertEquals(2, o.getConsumers().size()); + + em.clear(); + Owner lookedUp = (Owner)em.find(Owner.class, o.getId()); + assertEquals(2, lookedUp.getConsumers().size()); + } } diff --git a/proxy/code/src/org/fedoraproject/candlepin/test/TestUtil.java b/proxy/code/src/org/fedoraproject/candlepin/test/TestUtil.java index 30f8e85..a000d43 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/test/TestUtil.java +++ b/proxy/code/src/org/fedoraproject/candlepin/test/TestUtil.java @@ -56,8 +56,11 @@ public class TestUtil { * @return Consumer */ public static Consumer createConsumer() { - return createConsumer(new ConsumerType("test-consumer-type-" + randomInt()), - createOwner()); + return createConsumer(createConsumerType(), createOwner()); + } + + public static ConsumerType createConsumerType() { + return new ConsumerType("test-consumer-type-" + randomInt()); } public static int randomInt() { -- cgit