-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to handle this type of many-to-many relationship?
PostPosted: Mon Nov 07, 2005 7:25 pm 
Regular
Regular

Joined: Wed Aug 25, 2004 7:40 pm
Posts: 65
I have a many-to-many relationship in the same scenario as the followings:

A person has a set of characters, such as smart, thoughtful, tall, slim, etc.

The character set is not pre-definited in the system. When a user submits a person information, this user must also provide this person's character. If the character description such as "smart" is not in the system (DB), it will be saved into the system. I am not sure how to handle this object creation process with Hibernate in the right way. My thought is to save the character set data first and save the person object. I can think at lease two approachs here. One is to have the unique constraint on the character description and save the character set. This approach might not work with a batch save operation. The second approach I can think of is that to retrieve the character set data from DB first and only save those not in the DB. But, this approach will make the implementation quite complicated.

Any thought in this situation?

Thanks very much for any of your inputs.



Hibernate version: 3.0.x

Mapping documents:

<hibernate-mapping auto-import="true" default-lazy="false">

<class name="com.xyz.myaoo.domain.Person" table="persons">
<id name="id" type="int" unsaved-value="null" >
<generator class="native">
<param name="sequence">persons_id_seq</param>
</generator>
</id>
<set name="charactersInternal" table="personse_characters">
<key column="person_id"/>
<many-to-many column="character_id" class="com.xyz.myapp.domain.Character"/>
</set>
...
</class>
...
</hibernate-mapping>

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

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 7:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Have a look at the docs regarding cascading of associated data. Depending upon the complexity of the associated data (is it really just strings?) and your requirements for how this is used, you might also be able to use a "value collection" as opposed to an "entity collection"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 7:33 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you just want these properties to be saved as distinct objects each time, make them a collection (probably a list): you'll then have a different row in the table for each "smart" in the system, for each "considerate", etc.

If you want to provide users with a list of all existing person-properties, in the hopes that they won't enter "clever" if "smart" is already in there, then you'll want a many-to-many mapping, with PersonProperty being its own class. You'll need to write business logic to ensure that PersonProperties are looked up before new Persons are saved; if the value isn't in the PersonProperty table then you'll want to add it and put the new object into the Person's list of PersonProperties. This solution ensures that there will only be one "smart" row in the PersonProperty table. Of course, it's up to to ensure that there are no rows like "Smart", "smart ", etc.

Either way, this isn't really a hibernate issue. Deisgn your solution as if you were using plain old JDBC (or even just SQL): whatever your solution, you'll be able to model it using hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 09, 2005 12:03 am 
Regular
Regular

Joined: Wed Aug 25, 2004 7:40 pm
Posts: 65
steve wrote:
Have a look at the docs regarding cascading of associated data. Depending upon the complexity of the associated data (is it really just strings?) and your requirements for how this is used, you might also be able to use a "value collection" as opposed to an "entity collection"


Thanks very much Steve for your information in regarding of "value collection".

After studying the chapter 2.3.4, I modified all related code, mapping files, and DB schema. And my today's test result is good. The "value collection" meets my need. If I only look at the DB table, it is all I need.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 09, 2005 12:11 am 
Regular
Regular

Joined: Wed Aug 25, 2004 7:40 pm
Posts: 65
tenwit wrote:
If you just want these properties to be saved as distinct objects each time, make them a collection (probably a list): you'll then have a different row in the table for each "smart" in the system, for each "considerate", etc.

If you want to provide users with a list of all existing person-properties, in the hopes that they won't enter "clever" if "smart" is already in there, then you'll want a many-to-many mapping, with PersonProperty being its own class. You'll need to write business logic to ensure that PersonProperties are looked up before new Persons are saved; if the value isn't in the PersonProperty table then you'll want to add it and put the new object into the Person's list of PersonProperties. This solution ensures that there will only be one "smart" row in the PersonProperty table. Of course, it's up to to ensure that there are no rows like "Smart", "smart ", etc.

Either way, this isn't really a hibernate issue. Deisgn your solution as if you were using plain old JDBC (or even just SQL): whatever your solution, you'll be able to model it using hibernate.


Thanks tenwit for your input. I was trying to learn the Hibernate "language" for the case.


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