I've read in Hibernate docs that using inverse="true" on the one side of many-to-many relationship and inverse="false" on the other prevents from inserting two identical rows to relationship table. I've wrote a test application just to get on with it and, to my surprise, I got two inserts instead one. Could anybody explain this? Thnx in advance...
Association mappings are as follows:
<set name="applications" inverse="true" table="hb_application_developers">
<key column="developer_id"/>
<many-to-many column="application_id" class="pl.test.ApplicationHb"/>
</set>
on the one side and:
<set name="developers" inverse="false" table="hb_application_developers">
<key column="application_id"/>
<many-to-many column="developer_id" class="pl.test.DeveloperHb"/>
</set>
on the other. The code does above:
Session session = HibernateUtil.getSession(); //obtains session from SessionFactory
Transaction tx = session.beginTransaction();
ApplicationHb application = new ApplicationHb("XyberMech");
DeveloperHb developer = new DeveloperHb("A.Solecki");
session.save(application);
session.save(developer);
session.flush();
application.getDevelopers().add(developer);
developer.getApplications().add(application);
session.update(application);
HibernateUtil.closeSession(session, tx);//commits transaction and closes session
And the result SQL is:
select nextval ('hibernate_sequence')
select nextval ('hibernate_sequence')
insert into hb_application (title, id) values ('XyberMech', 546555)
insert into hb_developer (name, id) values ('A.Solecki', 546556)
insert into hb_application_developers (application_id, developer_id) values (546555, 546556)
insert into hb_application_developers (developer_id, application_id) values (546556, 546555)
On the other hand, when I set both sides to inverse=false or both sides to inverse="true", everything works fine o_O.
I use Hibernate 3.0 and Postgres 7.4 .
|