summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDevan Goodwin <dgoodwin@redhat.com>2009-12-03 15:39:37 -0400
committerDevan Goodwin <dgoodwin@redhat.com>2009-12-03 15:39:37 -0400
commit3a30375aabcfc58a2ca83381caec4079f2249c7e (patch)
treef9e69b819774fcc159c36b1932b17f97e0559fe6
parent6d48f05d2582b68e842b26539d072aad67292b53 (diff)
downloadcandlepin-3a30375aabcfc58a2ca83381caec4079f2249c7e.tar.gz
candlepin-3a30375aabcfc58a2ca83381caec4079f2249c7e.tar.xz
candlepin-3a30375aabcfc58a2ca83381caec4079f2249c7e.zip
Make owner -> consumer bi-directional relationship.
Though you aparently still have to add the object references on both sides. :(
-rw-r--r--proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java2
-rw-r--r--proxy/code/src/org/fedoraproject/candlepin/model/Owner.java38
-rw-r--r--proxy/code/src/org/fedoraproject/candlepin/model/test/OwnerTest.java28
-rw-r--r--proxy/code/src/org/fedoraproject/candlepin/test/TestUtil.java7
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<Consumer> consumers;
+ @OneToMany(mappedBy="owner", targetEntity=Consumer.class)
+ private Set<Consumer> consumers;
// EntitlementPool is the owning side of this relationship.
@OneToMany(mappedBy="owner", targetEntity=EntitlementPool.class)
@ForeignKey(name="fk_user_owner_id")
- private List<EntitlementPool> entitlementPools;
+ private Set<EntitlementPool> entitlementPools;
@Transient
- private List<User> users;
+ private Set<User> users;
/**
* Default constructor.
*/
public Owner() {
+ consumers = new HashSet<Consumer>();
+ entitlementPools = new HashSet<EntitlementPool>();
+ users = new HashSet<User>();
}
/**
@@ -77,6 +80,10 @@ public class Owner {
*/
public Owner(String nameIn) {
this.name = nameIn;
+
+ consumers = new HashSet<Consumer>();
+ entitlementPools = new HashSet<EntitlementPool>();
+ users = new HashSet<User>();
}
/**
@@ -110,37 +117,37 @@ public class Owner {
/**
* @return the consumers
*/
- public List<Consumer> getConsumers() {
+ public Set<Consumer> getConsumers() {
return consumers;
}
/**
* @param consumers the consumers to set
*/
- public void setConsumers(List<Consumer> consumers) {
+ public void setConsumers(Set<Consumer> consumers) {
this.consumers = consumers;
}
/**
* @return the entitlementPools
*/
- public List<EntitlementPool> getEntitlementPools() {
+ public Set<EntitlementPool> getEntitlementPools() {
return entitlementPools;
}
/**
* @param entitlementPools the entitlementPools to set
*/
- public void setEntitlementPools(List<EntitlementPool> entitlementPools) {
+ public void setEntitlementPools(Set<EntitlementPool> entitlementPools) {
this.entitlementPools = entitlementPools;
}
/**
* @return the users
*/
- public List<User> getUsers() {
+ public Set<User> getUsers() {
return users;
}
/**
* @param users the users to set
*/
- public void setUsers(List<User> users) {
+ public void setUsers(Set<User> 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<User>();
+ this.users = new HashSet<User>();
}
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<Consumer>();
- }
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<EntitlementPool>();
+ this.entitlementPools = new HashSet<EntitlementPool>();
}
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() {