-->
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: Can composite-elements be referenced in other mappings?
PostPosted: Sun Dec 06, 2009 7:26 am 
Regular
Regular

Joined: Mon Mar 10, 2008 6:40 pm
Posts: 114
I have a situation similar to this election schema:

Person
-------
Id
Name

Election
---------
Id
Date

Candidate
-----------
Person_Id
Election_Id
Party

So my hibernate mapping for Election would look like this:
Code:
<hibernate-mapping>
    <class name="com.example.Election" table="Election">
        <id name="id" column="Id" type="long"><generator class="native"/></id>
        <property name="date" type="timestamp" not-null="false"/>
        <set name="candidates" cascade="save-update" table="Candidate">
            <key column="Election_Id"/>
            <composite-element class="com.example.Candidate">
                <property name="party" length="255" not-null="false"/>
                <many-to-one name="person" class="com.example.Person" column="Person_Id" not-null="true"/>
            </composite-element>
        </set>
    </class>
</hibernate-mapping>

My question is, can I have something like person.getCandidates() where it returns all the Candidate objects that person was in? So normally in the Person hibernate mapping file I would have:
Code:
        <set name="candidates" cascade="save-update">
            <key column="Person_Id"/>
            <one-to-many class="com.example.Candidate"/>
        </set>

but the problem is that Candidate is not an entity. It's just a composite-element. So one solution is to just make it an entity and add an id field to Candidate. But that seems ugly. The table really never needs an id field. The SOLE PURPOSE of adding that id field would simply be because either hibernate isn't capable of mapping it properly or I don't know how to make hibernate map this.


Top
 Profile  
 
 Post subject: Re: Can composite-elements be referenced in other mappings?
PostPosted: Sun Dec 06, 2009 3:08 pm 
Newbie

Joined: Wed Jul 30, 2008 1:03 am
Posts: 16
Hi,

I'd have modeled the domain classes differently. You have a Person class. You have an Election class. That's fine. But a Candidate _is a_ Person who _has a_ association with an Election (which is a ManyToMany relationship). If you do it this way, then you can get the Candidates for a given Election, either by an HQL in your dao or by a bi-directional mapping between Candidates and Elections.

Hope it helps.
Ramin


Top
 Profile  
 
 Post subject: Re: Can composite-elements be referenced in other mappings?
PostPosted: Sun Dec 06, 2009 3:44 pm 
Regular
Regular

Joined: Mon Mar 10, 2008 6:40 pm
Posts: 114
ramin101 wrote:
a Candidate _is a_ Person who _has a_ association with an Election (which is a ManyToMany relationship). If you do it this way, then you can get the Candidates for a given Election, either by an HQL in your dao or by a bi-directional mapping between Candidates and Elections

Thank you for the response Ramin. So it definitely makes sense to have a many to many relationship between Person and Election here. The mapping for Person would look like:
Code:
        <set name="electionsCandidateIn" table="Person_in_Election" cascade="save-update">
            <key column="Person_Id"/>
            <many-to-many column="Election_Id" class="com.example.Election"/>
        </set>

But I need extra properties for the candidate, like party affiliation this person ran as in that particular election. Is there a way to add properties to the many-to-many relationship? Is this what you're referring to?


Top
 Profile  
 
 Post subject: Re: Can composite-elements be referenced in other mappings?
PostPosted: Sun Dec 06, 2009 9:46 pm 
Newbie

Joined: Wed Jul 30, 2008 1:03 am
Posts: 16
Actually what i meant was that there is an inheritance relationship between Candidate and Person; and there is a ManyToMany relationship between Candidate and Election. So basically there is no direct relationship between a Person and Election in this model (unless you want to capture his Vote and/or his Participation in the Election, but that's a different role).

For the (xml) mapping of a ManyToMany relationship, take a look at the documentation.


Top
 Profile  
 
 Post subject: Re: Can composite-elements be referenced in other mappings?
PostPosted: Mon Dec 07, 2009 1:54 am 
Regular
Regular

Joined: Mon Mar 10, 2008 6:40 pm
Posts: 114
ramin101 wrote:
Actually what i meant was that there is an inheritance relationship between Candidate and Person; and there is a ManyToMany relationship between Candidate and Election. So basically there is no direct relationship between a Person and Election in this model (unless you want to capture his Vote and/or his Participation in the Election, but that's a different role).

Maybe I should've chosen a better example, but Candidate is not a Person. It doesn't inherit from Person. A person can be a candidate in an election. They can be 10 candidates in 10 different elections, with different party affiliations for different elections. There is a one-to-many relationship between Person and Candidate. Maybe they were a republican for their first election and changed to be a democrat when they were a candidate in their next election.

ramin101 wrote:
For the (xml) mapping of a ManyToMany relationship, take a look at the documentation.

I gave an example of a correct xml mapping of the many-to-many relationship, I was wondering if there was a way to add extra fields in the relationship itself.


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.