-->
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.  [ 4 posts ] 
Author Message
 Post subject: Claws without a Cat
PostPosted: Sat Oct 16, 2004 6:49 am 
Newbie

Joined: Tue Oct 12, 2004 6:28 am
Posts: 6
[b]Hibernate version:[Hibernate 2.1.6]

[b]Mapping documents:[Claw + Cat]
Cat has many Claws

[color=red]
<hibernate-mapping>
<!-- Cat root -->
<class name="Cat" table="Cat">
<id name="id" column="id">
<generator class="identity"/>
</id>
<property name="name" column="name" type="string"/>
<property name="sex" column="sex" type="char"/>
<property name="weight" column="weight" type="double"/>
<set name="claws" table="CLAW" lazy="true">
<key column="cat_id"/>
<one-to-many class="Claw"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>

<!-- Claw root -->
<class name="Claw" table="Claw">
<id name="id" column="claw_id">
<generator class="identity"/>
</id>
<many-to-one name="owner" column="cat_id"/>
<property name="index" column="order_no" type="int"/>
<property name="tractable" column="tractable" type="true_false"/>
<property name="length" column="length" type="int"/>
</class>
</hibernate-mapping>
[/color]

My issue is from understanding Hibernate in applying referential integrity. When trying to delete Cats from the table I am noticing that even though I have a many-to-one relationship with Cat in Claw. I am still able to remove the objects without complaint from the Database. Leaving a Claws without a Cat (makes sense doesnt it :-)).The Claw Database has a single field that references the Cat table via its ID.

How can I apply this in my code?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 6:51 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Not in code, but you should just add a NOT NULL constraint to the CAT_ID FK column in the CLAWS table.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 17, 2004 3:06 am 
Newbie

Joined: Tue Oct 12, 2004 6:28 am
Posts: 6
christian wrote:
Not in code, but you should just add a NOT NULL constraint to the CAT_ID FK column in the CLAWS table.


Thanks for the response.

Im using hsqldb as my database so Im not sure how ti make the changes you mentioned (the database manager is very simple). What I was thinking is that some changes need to made in the hbm.xml docs so that the tables can be modified.

In addition to my previous post I would like to add a question about having a one-to-one relationship with another class e.g. (Cat and Owner). How would I be able to reflect this in the xml.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 17, 2004 5:18 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The documentation has examples for all of this, please consult it.

<many-to-one name="owner" column="cat_id" not-null="true"/>

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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