Versioning where collections are changed

Description

Customer has lazy inverse collection of Accounts. When this collection is changed Customer's version number should be incremented or not.

This test case shows that it depends on collection is initialized or not. In first transaction new Account is created and added to Customer without collection initialization. Version number stays unchanged. Second transaction does the same but it initializes Accounts collection using size() method. And here version number is incremented. So this is not consistent behaviour.

----------------------------------------------------------------------
MAPPINGS:

<hibernate-mapping>

<class
name="vo.AccountVO"
table="ACCOUNTS"
proxy="vo.AccountVO"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>

<id
name="ID"
column="ID"
type="long"
>
<generator class="assigned">
</generator>
</id>

<many-to-one
name="customer1"
class="vo.CustomerVO"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="CUSTOMER_ID"
/>

</class>

</hibernate-mapping>

<hibernate-mapping>
<class
name="vo.CustomerVO"
table="CUSTOMERS"
proxy="vo.CustomerVO"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>

<id
name="ID"
column="ID"
type="long"
>
<generator class="assigned">
</generator>
</id>

<version name="version" column="VERSION" type="integer"/>

<bag
name="accounts"
lazy="true"
inverse="true"
cascade="delete"
>

<key
column="CUSTOMER_ID"
>
</key>

<one-to-many
class="vo.AccountVO"
/>
</bag>

</class>

</hibernate-mapping>

----------------------------------------------------------------------
CODE:

transaction = session.beginTransaction();

CustomerVO c = (CustomerVO) session.get(CustomerVO.class,new Long(10));
System.out.println("Version: " + c.getVersion());

AccountVO a = new AccountVO();
a.setID(new Long(17));
a.setCustomer1(c);
c.getAccounts().add(a);
session.save(a);

transaction.commit();

transaction = session.beginTransaction();

c = (CustomerVO) session.get(CustomerVO.class,new Long(10));
System.out.println("Version: " + c.getVersion());

a = new AccountVO();
a.setID(new Long(18));
a.setCustomer1(c);
c.getAccounts().add(a);
c.getAccounts().size();
session.save(a);

transaction.commit();

transaction = session.beginTransaction();

c = (CustomerVO) session.get(CustomerVO.class,new Long(10));
System.out.println("Version: " + c.getVersion());

transaction.commit();

session.close();

----------------------------------------------------------------------
OUTPUT:

Hibernate: select customervo0_.ID as ID0_, customervo0_.VERSION as VERSION0_ from CUSTOMERS customervo0_ where customervo0_.ID=?
Version: 4
Hibernate: insert into ACCOUNTS (CUSTOMER_ID, ID) values (?, ?)
Version: 4
Hibernate: select accounts0_.CUSTOMER_ID as CUSTOMER2__, accounts0.ID as ID_, accounts0.ID as ID0_, accounts0_.CUSTOMER_ID as CUSTOMER2_0_ from ACCOUNTS accounts0_ where accounts0_.CUSTOMER_ID=?
Hibernate: insert into ACCOUNTS (CUSTOMER_ID, ID) values (?, ?)
Hibernate: update CUSTOMERS set VERSION=? where ID=? and VERSION=?
Version: 5

----------------------------------------------------------------------

Environment

Windows XP, Oracle 9i, Java 1.4.2_05

Assignee

Unassigned

Reporter

Lukasz Michonski

Labels

None

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Suitable for new contributors

None

Pull Request

None

backportDecision

None

backportReEvaluate

None

Components

Affects versions

Priority

Major
Configure