I have two tables as follows:
Code:
<class name="com.patix.pojo.Animal" table="Animal" catalog="patix">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity"></generator>
</id>
<set name="animalAttributes" inverse="true" cascade="all" table="AnimalAttribute">
<key>
<column name="animalId" not-null="true" unique="true" />
</key>
<many-to-many column="attributeId" class="com.patix.pojo.Attribute" />
</set>
</class>
<class name="com.patix.pojo.Attribute" table="Attribute" catalog="patix">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity"></generator>
</id>
<set name="animalAttributes" inverse="true" cascade="all" table="AnimalAttribute">
<key>
<column name="attributeId" not-null="true" unique="true" />
</key>
<many-to-many column="animalId" class="com.patix.pojo.Animal" />
</set>
</class>
I have a form and I am displaying the items in the Attributes table to the user, so he can select the ones he wants. then I would like to add a new animal with the attributes the user has chosen.
What I do is, I create a new animal object and add the attributes to the animalAttributes set of the object, and save. Hibernate inserts the new animal to the animals table, but it doesnt update the junction table AnimalAttribute. from the logs I see "object already associated with session: [com.patix.pojo.Attribute]" so I am guessing that hibernate is not updating the junction table, because I'm not adding a new attribute, but I am just trying to create a new relation.
What do I need to do to make hibernate update the junction table by adding the relations between the new animal and attributes that already exist.
Thanks