-->
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.  [ 2 posts ] 
Author Message
 Post subject: how to implement special one-to-one or one-to-none
PostPosted: Fri Jul 08, 2005 9:07 pm 
Newbie

Joined: Fri Jul 08, 2005 8:41 pm
Posts: 2
How to implement the query , *.hbm.xml and java about special one-to-one or one-to-none as the below.


There is Cat, Cat could have one child or none child depending on the Child_id value.

The Database table name : Cat

desc Cat;
Id NUMBER NOT NULL UNIQUE;
Name Varchar2(32) NOT NULL;
Child_id NUMBER;


select * from Cat;
Id Name Child_id
-----------------------------------------------------
1000 Kitty 1001
1001 childKitty 1002
1002 grandchildkitty 1003
1003 kitty3 <null>
2000 Polo 1001
2001 Polo2 <null>
3001 Lina <null>

The Child_id relates with Id

What I want to get is that:
If I query Id:1000 (Kitty) once, the all data related could be returned, which includes childKitty and grandchildkitty.

Does anyone tell me how to implement it?








Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.0.2

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using: oracle 8.1.7

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 08, 2005 10:04 pm 
Beginner
Beginner

Joined: Fri Jul 08, 2005 12:38 pm
Posts: 41
Location: Massachusetts, USA
Try a one-to-one association with property-ref="childId".

Code:
<class name="Cat" table="CAT">
    <id name="id" column="ID">
        ...
    </id>

    <property type="string" name="name" column="NAME"/>

    <one-to-one name="child"
        class="Cat"
        lazy="false"
        property-ref="childId"/>
</class>


I think you can get this to work.

However, the design would be more flexible, and follow a more common pattern, if a Cat referenced its parent. This allows a Cat to have more than one child. If you need to limit the number of children, that can be done easily in your Java code (the setChildren method would throw an exception if the set has more than one item in it). This is a simple one-to-many relationship.[/code]


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