-->
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.  [ 7 posts ] 
Author Message
 Post subject: one-to-one mapping on the same table
PostPosted: Tue Jul 13, 2004 9:29 am 
Newbie

Joined: Tue Jul 13, 2004 8:54 am
Posts: 3
I need to map a parent child relationship, to a single table, both parent and child need to use the same primary key and the java code should be generated from the hbm.xml files. I am using hibernate 2.1 and hbm2java code generator

Parent.hbm.xml

Code:
<hibernate-mapping>
    <class name="mapping.Parent" table="MAPPINGTEST">

        <id name="id" type="java.lang.Integer" column="ID">
            <generator class="sequence">
                 <param name="sequence">MAPPINGTEST_ID_SEQ</param>
             </generator>
        </id>
   <property
            name="firstName"
            type="java.lang.String"
            column="FNAME"
            length="25"
        />
        <property
            name="lastName"
            type="java.lang.String"
            column="LNAME"
            length="50"
        />
        <property
            name="age"
            type="java.lang.Integer"
            column="AGE"
            length="11"
        />
    </class>
</hibernate-mapping>


Child.hbm.xml
"This persistent object does not have its own primary key, it should be inherited from the parent"

Code:
<hibernate-mapping>
   <class name="mapping.Child" table="MAPPINGTEST">
        <meta attribute="extends">
            mapping.Parent
        </meta>
        <id type="java.lang.Integer" column="ID">
            <generator class="foreign">
                <param name="table">MAPPINGTEST</param>
                <param name="column">ID</param>
            </generator>           
        </id>
        <one-to-one
            name="Parent"
            class="mapping.Parent"
            cascade="none"
            constrained="true"
        />
        <property
            name="firstLineAdd"
            type="java.lang.String"
            column="address1"
            length="30"
        />
        <property
            name="secondLineAddress"
            type="java.lang.String"
            column="address2"
            length="30"
        />
    </class>
</hibernate-mapping>


Is there a way I can get hibernate to use the parent objects <id> field as the child's primary key? What it seems to be doing at this point is creating a new id value instead of inheriting the parents field.
I also can use the <subclass> tag as I don't have a discriminator column available.

Many Thanks
Marc


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 13, 2004 9:32 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
are you sure this is not a many-to-one that you need even if in reality you'll always have only one child?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 13, 2004 10:41 am 
Regular
Regular

Joined: Mon Jun 14, 2004 1:42 pm
Posts: 80
Location: Brazil
suppouse it is many-to-one that you need even if in reality you'll always have only one child. Is There a way to the O-R abstraction hide it from the class implementation? Do I have to code a Set to represent it !? Is it right? in the conceptual way I think it is. A legacy database misconception should be abstracted in the O-R.
Maybe if he stop using the code generator, and make the class by hand. Is There a way to map 1-1 class relation to 1-n database relationship?
Does it involve complex UserType definitions?
I'm trying to discover how to do that too.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 13, 2004 10:45 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
many to one doesn't mean there is a set (it is to -one...).
If it is not bidirectionnal, you'll have exactly what you want

shaow db schema and javaBean you expect to have, we'll say if it is possible

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 13, 2004 12:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I think what you are looking for is <component>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 14, 2004 4:40 am 
Newbie

Joined: Tue Jul 13, 2004 8:54 am
Posts: 3
OK, I have had a go with the many-to-one child version and have moved forward. Parent.hbm.xml remains the same as above.

child.hbm.xml
Code:
<hibernate-mapping>
    <class name="mapping.Child" table="MAPPINGTEST">
        <meta attribute="extends">
            mapping.Parent
        </meta>
        <id type="java.lang.Integer" column="ID">
            <generator class="foreign">
                <param name="property">Parent</param>
            </generator>           
        </id>
        <many-to-one
            name="Parent"
            class="mapping.Parent"
            column="ID"
            update="false"
            insert="false"
        />
        <property
            name="firstLineAdd"
            type="java.lang.String"
            column="address1"
            length="30"
        />
        <property
            name="secondLineAddress"
            type="java.lang.String"
            column="address2"
            length="30"
        />
    </class>
</hibernate-mapping>


Hibernate does load the mapping file with no errors, but when I try retrieve the child object I get the following error.

Code:

net.sf.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: mapping.Child (loaded object was of wrong class)
   at net.sf.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:456)
   at net.sf.hibernate.loader.Loader.getRow(Loader.java:423)
   at net.sf.hibernate.loader.Loader.doQuery(Loader.java:209)
   at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
   at net.sf.hibernate.loader.Loader.doList(Loader.java:950)
   at net.sf.hibernate.loader.Loader.list(Loader.java:941)
   at net.sf.hibernate.loader.CriteriaLoader.list(CriteriaLoader.java:118)
   at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:3571)
   at net.sf.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:223)
   at com.lbss.framework.persistence.HibernateCriteria.retrieveIterator(HibernateCriteria.java:97)



I will look at using a <component> tag and see how it works.

Many Thanks
Marc


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 14, 2004 8:36 am 
Newbie

Joined: Tue Jul 13, 2004 8:54 am
Posts: 3
The <component> tag does not make use of inheritance as far as I can see, I am using the hbm2java code generator, which adds the parent object to the child as a member variable, I need to make use of Inheritance!

Can anybody help with the error code I am getting in the above post?

Cheers
Marc


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.