-->
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: OneToMany - unique java objects
PostPosted: Tue Sep 14, 2010 3:31 pm 
Newbie

Joined: Tue Sep 14, 2010 3:08 pm
Posts: 3
I have an object that contains a list of options. The list of options is represented as a OneToMany relationship. Each Option then has a OneToMany relationship to values via a Join Table as below:

Code:
    @OneToMany(fetch=FetchType.EAGER)
    @JoinTable(name="OptionValue",
        joinColumns = @JoinColumn(name="option_id", referencedColumnName="id"),
        inverseJoinColumns = @JoinColumn(name="externalId", referencedColumnName="partNumber")
    )
    @IndexColumn(name = "position")
    @XStreamOmitField
   private List<ValueItem> values;


An individual Option may have values that point to the same OptionValue entity. However when the values list of the Option are loaded I would like unique Java objects for each ValueItem.

Code:
P -> Options |--> Opt1 -> Values |-> Val1
             |                   |-> Val2
             |                   |-> Val3
             |
             |---> Opt2-> Values |-> Val1
             |                   |-> Val3

I would like Val1, Val2, Val3 to be unique objects between Opt1 and Opt2. So Val1 and Val3 are different Java objects between Opt1 and Opt2. Is this possible?


Top
 Profile  
 
 Post subject: Re: OneToMany - unique java objects
PostPosted: Tue Sep 14, 2010 4:20 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
No it's not. A main feature of Hibernate is that, within the scope of a session, it guarantees that there is only one object representing a single entity: http://docs.jboss.org/hibernate/stable/ ... s-identity

I also find it a bit strange that you map the values as @OneToMany. The structure in your post clearly indicates that it is actually a @ManyToMany. If you let Hibernate generate the schema for a @OneToMany association a unique constraints will be added to the externalId column which means that a value can only be used for one option. Eq. it would not be possible to have Val1 and Val3 as values for both Opt1 and Opt2.


Top
 Profile  
 
 Post subject: Re: OneToMany - unique java objects
PostPosted: Tue Sep 14, 2010 4:30 pm 
Newbie

Joined: Tue Sep 14, 2010 3:08 pm
Posts: 3
If I use a ManyToMany I will not get unique objects either?


Top
 Profile  
 
 Post subject: Re: OneToMany - unique java objects
PostPosted: Tue Sep 14, 2010 4:43 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Why do you want more than object representing the same entity?


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.