-->
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: Mapping a many-to-one relationship only in one side
PostPosted: Wed Oct 15, 2008 7:06 pm 
Newbie

Joined: Wed Oct 15, 2008 6:51 pm
Posts: 2
Hi,

I have a mapping question I would like to ask to all of you. I've read the documentation lots of times, and I have also tried to solve it by myself hundreds of times. I think it's time to try if someone can help me a bit.

I'm mapping two classes. Let's say Country and City. This is a trivial one-to-many relationship: one country has many cities, and a city is in a single country.

I map the country field in the City mapping like this:

<many-to-one name="country" class="Country" column="countryId"/>

And then, I map the Set of cities in the Country mapping:

<set name="cities" inverse="true">
<key column="id"/>
<one-to-many class="City"/>
</set>

So, the country has a list of cities, and the city has a Country. All this works fine.

But the problem comes when I don't want the country to have a list of cities inside of it. I mean, I just want the city to know which country is it in, but I want the country to have no idea about cities.

If I delete the "set" tag in the country mapping, IntelliJ Idea's Hibernate Validator tells me: "'<many-to-one>' attribute target should be Entity".

I know there must be a way to have a many-to-one relationship without taking care of it in one side... but I can't see how.

Can somebody help me?

Thanks in advance,
Roger.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2008 4:54 am 
Beginner
Beginner

Joined: Tue Sep 09, 2008 5:42 am
Posts: 22
Location: Romania
Ciao,

Go to this link: http://forum.hibernate.org/viewtopic.php?t=991561&highlight=

and you can see how I suggested to map the two tables. Substitute the Tab2 table (from the example in the link) with Country and Tab1 table with City and you have your problem solved.

Good luck!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2008 7:31 am 
Newbie

Joined: Wed Oct 15, 2008 6:51 pm
Posts: 2
I've tried your solution:

I've changed

<many-to-one name="country" class="Country" column="countryId"/>

for

<many-to-one name="country" class="Country">
<column name="countryId"/>
</many-to-one>

But it didn't solve the problem. It stills wants me to have a set of cities in the country mapping.

Thanks a lot for taking time to answer!

Roger.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2008 8:18 am 
Beginner
Beginner

Joined: Tue Sep 09, 2008 5:42 am
Posts: 22
Location: Romania
I don't understand what is causing your problem. Try to add on the many-to-one relation the attributes fetch="select", not-found="ignore" or not-null="false". And delete the list of Cities from the Country class.

If none of these gives the expected result, please post the structure of your tables, the classes and the mapping files so I can try to replicate your situation and see why you have this problem.

I don't think I ever used the bag element to map on the other side the many-to-one relation and none of my application raised me an exception like you said.

Try and let me know!


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.