openjpa-users mailing list archives

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

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.
public class Test {
  @Column name="id_column"
  protected long idColumn;

  protected String columnA;

  @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.
public class TestChild1 extends Test {

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

// Second child.
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");  //

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:
Sent from the OpenJPA Users mailing list archive at

View raw message