-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: @OneToMany and inheritance problem
PostPosted: Wed May 17, 2006 10:59 am 
Newbie

Joined: Thu Jan 13, 2005 10:04 am
Posts: 18
I have mapped an inheritance hierarchy using the 'Joined subclasses' strategy for the classes ChildType1, ChildType2 and Parent (ChildType1 and ChildType2 extends Parent). The subclasses have a primary key association to the Parent superclass making it a one-to-one association. There is another class that I am calling A. A has a bidirectional one-to-many relationship with ChildType1 and ChildType2.

Code:
@Entity
class A {
    List<ChildType1> childrenType1 = new ArrayList<ChildType1>;
    List<ChildType2> childrenType2 = new ArrayList<ChildType2>;

    @OneToMany(mappedBy="a", targetEntity=Parent.class, fetch = FetchType.LAZY)
    @org.hibernate.annotations.Cascade( {
        org.hibernate.annotations.CascadeType.ALL,
        org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @IndexColumn(base=0, nullable=false, name="idx")
    public List<ChildrenType1> getChildrenType1();

    @OneToMany(mappedBy="a", targetEntity=Parent.class, fetch = FetchType.LAZY)
    @org.hibernate.annotations.Cascade( {
        org.hibernate.annotations.CascadeType.ALL,
        org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @IndexColumn(base=0, nullable=false, name="idx")
    public List<ChildrenType2> getChildrenType2();
}


@Entity
@Inheritance(strategy=InheritanceType.JOINED)
abstract class Parent {
    A a;
    @ManyToOne
    @JoinColumn(name="some_fk", nullable=false)   
    public A getA();

}

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name="id")
class ChildType1 extends Parent {
     ... 
}

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name="id")
class ChildType2 extends Parent {
     ... 
}



When I call getChildrenType1 I noticed the following hibernate query was generated (I have changed the names of the classes used and removed the as value_0_, essentially I have the 2 subclasses and the superclass mapped using the 'table-per-subclass':

Code:
select child0_.some_fk, child0_1_.value, child0_2_.value,
case
when child0_1_.id is not null then 1
when child0_2_.id is not null then 2
when child0_.id is not null then 0
end as clazz_0_ from

Parent child0_ left outer join ChildType1 child0_1_ on child0_.id=child0_1_.id
left outer join ChildType2 child0_2_ on child0_.id=child0_2_.id
where child0_.some_fk=?


I want to ONLY get the ChildType1 back - I can't see why the case statement is necessary.

Some help would be very welcome. I have read and re-read the documentation and the forums. I am starting to think that perhaps mapping an inheritance hierarchy is more trouble than it's worth (that is unless I am doing something wrong)...


Last edited by paulf on Wed May 17, 2006 12:07 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 11:53 am 
Newbie

Joined: Thu Jan 13, 2005 10:04 am
Posts: 18
Furthermore - when I try and iterate over the returned List I get the following exception:

Code:
java.lang.ClassCastException: ChildType2
   at test.testLoadA(MyTest.java:113)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)



Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 12:30 pm 
Newbie

Joined: Thu Jan 13, 2005 10:04 am
Posts: 18
Maybe I should be asking if anyone has mapped a List<of some subclass> successfully (including being able to add and remove entries)?

I am reading the documentation and have spent the last few days looking at this problem - I have tried. Please Help!

Regards,

Paul


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 12:45 pm 
Newbie

Joined: Thu Jan 13, 2005 10:04 am
Posts: 18
If I change the annotation @JoinColumn to:
Code:
@JoinColumn(name="some_fk", table="parent")


I get the following exception:
Code:
org.hibernate.cfg.NotYetImplementedException: Collections having FK in secondary table


Maybe this is the reason I am unable to do this?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 19, 2006 6:00 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The problem is that the mappedBy property you refers to actually belongs to the superclass, not the subclass.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.