Greetings!
This seems to be a trivial issue, but I didn't manage to find a solution with the documentation, google and my colleagues - hence this post. I prepared a simplified test case for the problem I'm experiencing with a more complicated model. Let's start with general information and mapping files:
Hibernate version: 3.2.6.ga
Database engines: HSQLDB / Oracle 9i
Mapping documents:
Transaction:
Code:
<hibernate-mapping>
<class name="Transaction" table="Transaction">
<id type="integer">
<generator class="increment"></generator>
</id>
<many-to-one name="Agent" class="Agent" update="false" insert="false" fetch="select">
<column name="ValueA" length="8" />
<column name="ValueB" length="5" />
</many-to-one>
<property name="code" type="string">
<column name="code" length="3" />
</property>
</class>
</hibernate-mapping>
Agent:
Code:
<hibernate-mapping>
<class name="Agent" table="Agent">
<composite-id>
<key-property name="valueA" type="string">
<column name="valueA" />
</key-property>
<key-property name="valueB" type="string">
<column name="valueB" />
</key-property>
</composite-id>
<set name="transactions" inverse="true">
<key>
<column name="ValueA" length="8" />
<column name="ValueB" length="5" />
</key>
<one-to-many class="Transaction" />
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
try {
session.beginTransaction();
Transaction trans = new Transaction();
Agent agent = new Agent();
agent.setValueA("agent value A");
agent.setValueB("agent value B");
trans.setCode("transaction code");
agent.getTransactions().add(trans);
trans.setAgent(agent);
session.save(agent);
session.save(trans);
session.getTransaction().commit();
} catch(HibernateException ex) {
session.getTransaction().rollback();
throw ex;
}
The generated SQL (show_sql=true):Code:
Hibernate: select max(id) from Transaction
Hibernate: insert into Agent (valueA, valueB) values (?, ?)
Hibernate: insert into Transaction (code, id) values (?, ?)
Now, onto the problem itself: as you can see in the SQL log, saving both objects generates two inserts, but values valueA and valueB are not inserted into the transaction table.
Of course, if I get the Transaction back from the database it has the reference to Agent set to null.
What did I miss here?
Thanks in advance,
Karol