After reading all the answers, there was no one who could talk about the inverse attribute of sleep mode.
In my opinion, you should also check in your relationship whether the reverse keyword is set correctly. The converse is designed to determine which party owns the relationship. The update and insertion procedure depends on this attribute.
Suppose we have two tables:
main_table , middle_table
with a ratio of one to many . The hiberntate mapping classes are Primary and Secondary, respectively.
So, the Principal class has a SET of Medium objects. The xml mapping file should look like this:
<hibernate-mapping> <class name="path.to.class.Principal" table="principal_table" ...> ... <set name="middleObjects" table="middle_table" inverse="true" fetch="select"> <key> <column name="PRINCIPAL_ID" not-null="true" /> </key> <one-to-many class="path.to.class.Middel" /> </set> ...
The inverse value is set to true, which means that the middle class is the owner of the relationship, so the Principal class will be NOT UPDATE .
So, the update procedure can be implemented as follows:
session.beginTransaction(); Principal principal = new Principal(); principal.setSomething("1"); principal.setSomethingElse("2"); Middle middleObject = new Middle(); middleObject.setSomething("1"); middleObject.setPrincipal(principal); principal.getMiddleObjects().add(middleObject); session.saveOrUpdate(principal); session.saveOrUpdate(middleObject);
It worked for me, you can suggest some editions to improve the solution. So we will all learn.
Luis Teijon Aug 21 '16 at 14:33 2016-08-21 14:33
source share