-->
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: Problem loading Set of subclass (Table per Subclass)
PostPosted: Tue May 16, 2006 6:20 am 
Newbie

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

Code:
@Entity
class A {
    Set<Child> children;

    @OneToMany(mappedBy="a") @IndexColumn(name="id")
    @JoinColumn(name="some_fk")
    public Set getChildren();
}


@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 Child extends Parent {
     ... 
}


I can successfully save an instance of Child and get the expected values in the two relevant tables and I can query using an explicit 'from Child' or polymorphic query 'from Parent' and get the correct values back.

I can also save an instance of A that has added an instance of Child to the Set children and everything looks fine, I get the correct values saved and the foreign and primary keys are as expected.

If, however, I load and instance of A (which works) and try and access the Set children I get an exception:

Code:
ERROR org.hibernate.util.JDBCExceptionReporter - Unknown column 'child0_.some_fk' in 'field list'

It would appear that Hibernate is trying to load the Set of Child expecting to find the foreign key 'some_fk' in the Child table, I have this mapping mapped via the @ManyToOne annotation in the Parent class and hence in the Parent table.

Am I able to map such a relationship? If so, any ideas where I am going wrong!


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 16, 2006 10:26 am 
Newbie

Joined: Thu Jan 13, 2005 10:04 am
Posts: 18
I found the solution, I added the propert targetEntity to the @OneToMany annotation:

Code:
@Entity
class A {
    Set<Child> children;

    @OneToMany(mappedBy="a", targetEntity=Parent.class)
    @IndexColumn(name="id")
    @JoinColumn(name="some_fk")
    public Set getChildren();
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 6:03 am 
Newbie

Joined: Thu Jan 13, 2005 10:04 am
Posts: 18
Help!

Well I had thought I had solved this problem but it would seem I am not quite there.

When I call getChildren 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 3 subclasses and the superclass mapped using the 'table-per-subclass':

Code:
select child0_.some_fk, child0_1_.value, child0_2_.value, child0_3_.value,
case
when child0_1_.id is not null then 1
when child0_2_.id is not null then 2
when child0_3_.id is not null then 3
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
left outer join ChildType3 child0_3_ on child0_.id=child0_3_.id
where child0_.some_fk=?


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

Also, I changed from Set<Child> to List<Child> as I would like to use the IndexColumn annotation as I believe this will help with deleting from the List - which I am unable to do. I can add to the List but when I try and remove an object from the List that was returned using the getChilldren method the only thing that updates is class A.

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)...


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 6:56 am 
Newbie

Joined: Thu Jan 13, 2005 10:04 am
Posts: 18
Okay - well I am getting somewhere. To solve the delete problem that I had I added the following annotation to the List<ChildType1> getChildren method:

Code:
@org.hibernate.annotations.Cascade( { org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })


and removed the cascade = CascadeType.ALL from the @OneToMany annotation.

I also removed the @IndexColumn as it was causing problems - I think I'll come back to this one later...

If anyone could help with why I get the rather complicated query (see previous post) when I call the getChildren method that would be great.

Is this expected behaviour?


Top
 Profile  
 
 Post subject: Problem: subclass and hibernate mappings, table name wrong
PostPosted: Tue May 30, 2006 9:10 pm 
Newbie

Joined: Tue May 30, 2006 8:45 pm
Posts: 1
I believe that I have a similar problem to this one, although I am using a mapping file and not using annotations. I have not found sufficient information in this thread to isolate my problem.

I am successfully using many-to-one unidirectional associations in Hibernate 3 to access my database objects as Java objects. I am trying to extend the hibernate mappings to add the reverse associations, with little success.

Specifically, I am trying to build a bi-directional many-to-one association based on a foreign key between 2 subclasses of a common base class. When I try to traverse the one-to-many side, I get an exception dump which includes "Unknown column 'imagesx0_.specimenId' in 'field list'" The table name used here is based on the name of the <set > mapping, not of the database table.

I believe the problem is that hibernate is choosing the wrong name for the related table. When I change the name of the set mapping, the error message changes. In the above, I had changed the name of the Set from "images" to "imagesX" and the error changed from "Unknown column 'images0_.specimenId'..." to "Unknown column 'imagesx0_.specimenId' ...".

The actual table name is "image"

Some details of the code and mapping file follow. The many-to-one direction of this mapping works properly, but not the reverse.

I would appreciate any ideas about what I've done wrong in this system. I can supply more details, if needed.

I have a mapping file with a base class (BaseObject) and 2 subclasses Image and Specimen, and tables baseObject, image and specimen. The image table has a specimenId field that is a foreign key to specimen. Both image and specimen have an "id" field that is a foreign key to baseObject.

The Java classes include

Code:
class BaseObject { String id ...}
class Specimen extends BaseObject {... Set imagesX;...}
class Image extends BaseObject {...Specimen specimen;...}


code fragment that causes error is:

Code:
getImagesX().iterator


The mapping files include

Code:
   <subclass name="net.morphbank.rdfmetadata.Specimen"
      extends="net.morphbank.rdfmetadata.BaseObject"
      discriminator-value="Specimen">
      <set name="imagesX" table="image" inverse="true" >
         <key column="specimenId" />
         <one-to-many class="net.morphbank.rdfmetadata.Image" />
      </set>

      <join table="SpecimenExtended">

... and...
Code:
      <subclass name="net.morphbank.rdfmetadata.Image" extends="net.morphbank.rdfmetadata.BaseObject"
         discriminator-value="Image">
         <join table="image">
            <key column="id" />
               </property>
            <many-to-one name="specimen" class="net.morphbank.rdfmetadata.Specimen" column="specimenId" />

...

_________________
G Riccardi
College of Information
Florida State University


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.