Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate: how do I code the mapping for a composite, unique key that isn't the primary key?
    text
    copied!<p>I have a file where I'm counting votes. I want to limit each user to a single vote on a given competition. The user can return and change his/her vote, but it would update the choice of competitor in the competition.</p> <p>I can't figure out how to do this using Hibernate mapping files. (The programming language is Java.)</p> <p>I've looked at using composite-id, but I want to have a typical numeric primary key on this thing if I need it later. (I also couldn't figure out how to make that work! =)</p> <p>Here's my mapping file from which I generate the model objects and SQL:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt; &lt;hibernate-mapping&gt; &lt;class name="com.example.project.model.db.Vote" table="vote"&gt; &lt;id name="voteId" type="int"&gt; &lt;meta attribute="scope-set"&gt;protected&lt;/meta&gt; &lt;meta attribute="use-in-equals"&gt;true&lt;/meta&gt; &lt;generator class="native" /&gt; &lt;/id&gt; &lt;many-to-one name="user" column="userId" unique="false" not-null="true" lazy="false" class="com.example.project.model.db.User" /&gt; &lt;many-to-one name="competition" column="competitionId" unique="false" not-null="true" lazy="false" class="com.example.project.model.db.Competition" /&gt; &lt;many-to-one name="competitor" column="competitorId" unique="false" not-null="true" lazy="false" class="com.example.project.model.db.Competitor" /&gt; &lt;property name="dateAdded" type="date" not-null="true" /&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload