openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nibbler <Michael.Schn...@VerizonWireless.com>
Subject Help - Single table inheritance not working with one-to-many collection
Date Sat, 06 Dec 2008 01:37:23 GMT

Hi,
I'm having trouble with single table inheritance and a one-to-many
relationship.  Specifically, I have two Entities that subclass from a
superclass using single table inheritance.  I have another Entity that has a
one-to-many relationship with one of the subclasses.  Using a simple Query,
they behave properly, I get one subclass or the other.  But when accessing
one of them through the container's one-to-many relationship, I get a
collection containing objects of both classes.

My classes basically look like this:

// This is the superclass.
@MappedSuperclass
@Table(name="test_table")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE
@DiscriminatorColumn(name="discriminator")
public class Test {
  @Id
  @Column name="id_column"
  protected long idColumn;

  @Basic
  @Column(name="column_a")
  protected String columnA;

  @Basic
  @Column (name="site_id")
  protected long siteId;

  // Here's the link to the containing Entity.
  @ManyToOne (fetch=FetchType.LAZY)
  @JoinColumn (name="containing_obj_id")
  private ContainingObject co;
...
}

// This is the first child.
@Entity
@DiscriminatorValue("S")
public class TestChild1 extends Test {

  @Basic
  @Column (name="column_b")
  private String columnB;
...
}

// Second child.
@Entity
@DiscriminatorValue("D")
public class TestChild2 extends Test {
  // This class doesn't have any unique properties.
}

// This class contains the one-to-many.
public class ContainingObject {
...
  // This is supposed to map a collection of TestChild1 objects.
  @OneToMany (mappedBy="co")
  private Set<TestChild1> testChildren = new HashSet<TestChild1>();
...
}

Okay, so let's say I have 3 TestChild1 and 2 TestChild2 rows in test_table
for a given ContainingObject. this works:
  Query q = em.createQuery("select test1 from TestChild1 test1 where
containing_obj_id = 100");
  List<TestChild1> list = (List<TestChild1>)q.getResultList();
  System.out.println("There are " + list.size() + " testChild1 objs");  //
THIS WILL SHOW THERE ARE 3.

But this doesn't work:
  // I've retrieved a ContainingObject 'co' with containing_obj_id of 100.
  Set<TestChild1> children = co.getTestChildren();
  System.out.println("There are " + children.size() + " testChild1 objs"); 
// THIS WILL SHOW all 5.

I have tried to put the many to one relationship in the TestChild1 class
(just to test) and that didn't work either.  What am I missing?

Thanks in advance!


-- 
View this message in context: http://n2.nabble.com/Help---Single-table-inheritance-not-working-with-one-to-many-collection-tp1620948p1620948.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Mime
View raw message