-->
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: Many to One Mapping
PostPosted: Mon Jan 01, 2007 10:06 am 
Newbie

Joined: Mon Jan 01, 2007 9:47 am
Posts: 1
I've got 2 Tables:

User & Awards

A User can have many awards.

I've mapped a Unidirectional Many to one mapping from Awards to user like this:


<many-to-one name="user" class="User"
fetch="select" >
<column name="user_id" not-null="true" />
</many-to-one>

I'd like to know how is it possible to delete the awards when a user is deleted?

Right now, it throws a foriegn key exception when I delete the User. I have nothing mentioning Awards in the User hbm file.


Newbie question:
For every column that is mapped in the hbm file, does there need to be a getter/setter for it (I know there needs to be, but is there a way not to?). The reason I ask is, by declaring the relationship in Awards, but nothing in user, I don't see how Hibernate will know there is a relationship between User & Awards.

However, I don't want to clutter my User table with the Awards data, I especially don't want to load the Awards when all I want to do is load the Users Data.

Thanks.


Top
 Profile  
 
 Post subject: Re: Many to One Mapping
PostPosted: Mon Jan 01, 2007 11:01 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
nvmurali wrote:
I'd like to know how is it possible to delete the awards when a user is deleted?

Since you have a unidirectional mapping, there is no way to cascade deletions from User to Awards. You need to manually remove the associations from the Awards table corresponding to the User being deleted. Doing a Query like
Code:
from Awards where user = :user

get the set of Awards, and make those users null.


nvmurali wrote:
Newbie question:
For every column that is mapped in the hbm file, does there need to be a getter/setter for it (I know there needs to be, but is there a way not to?). The reason I ask is, by declaring the relationship in Awards, but nothing in user, I don't see how Hibernate will know there is a relationship between User & Awards.

No need, if the access method is set to field, then there is no need for getter/setters. They are required only if the access method is set to property. But I dont see how thats connected to your question. You are only doing a unidirectional mapping, so there is no way for Hibernate to know the association from the other side[/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.