I am using Hibernate 1.3.1 in Spring. My database is Oracle 10g express.
I have the following object tree:
Portfolio {id, name, List<Positions> positions}
Position {id, symbol, nShares}
Here is the mapping file:
<hibernate-mapping auto-import="true" default-lazy="false">
<class name="Portfolio">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<list name="positions" inverse="false" cascade="all">
<key column="pos_id" not-null="true"/>
<list-index column="indx"/>
<one-to-many class="Position"/>
</list>
</class>
<class name=”Position”>
<id name="id">
<generator class="native"/>
</id>
<property name="symbol"/>
<property name=”nShare”/>
</class>
</hibernate-mapping>
The test code:
Test() {
Position pos = new Position();
pos.setId(1000L);
pos.setSymbol("bar");
pos.setNSahres(100);
Protfolio port = new Portfolio();
port.setId(1000L);
port.setName("port");
List<Positions> positions = new ArrayList<Position>(1);
positions.add(pos);
port.setIPositions(positions);
System.out.println("Test before save: " + port);
getHibernateTemplate().save(prod);
System.out.println("Test after save: " + port);
}
The output:
Test before save: Portfolio (port, 1000, [Position(bar, 1000, 100)]).
Test before save: Portfolio (port, 3, [Position(bar, 1000, 100)]).
As you can see that portfolio id has been changed after save while position id has not.
The table position is empty.
I found from trace that save() was called on portfolio but update() was called on positions.
If it called save() on both parent and child, I should not have seen the problem.
How should I define map files in order to achieve this behavior?
Thanks in advance,
Paul
|