openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jeremy Bauer" <techhu...@gmail.com>
Subject Re: Help - Single table inheritance not working with one-to-many collection
Date Mon, 08 Dec 2008 15:10:28 GMT
Hi,
I think the problem may be related to the use of MappedSuperclass on the
base class.  Is your intent to create an entity inheritance hierarchy?
 (This appears to be the case since you've specified an inheritance type and
discriminator.)  If so the intent of mapped superclass is to map the
superclass persistent information into the subclass entity table.  If you
want a true inheritance hierarchy you should tag your Test class with
@Entity instead of @MappedSuperclass.

I haven't verified, but OpenJPA may be getting confused by the @Table
annotation on the mapped superclass and is storing both types of entities in
a single table.  Normally, each entity would map to its own table and that
table would contain its persistent information and the persistent
information stored in the mapped superclass.

Hope this helps,

-Jeremy

On Fri, Dec 5, 2008 at 7:37 PM, nibbler <Michael.Schneir@verizonwireless.com
> wrote:

>
> 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
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message