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.