Hibernate version 4.3.5.Final.
Hibernate mapping:
An account contains a list of entries:
Code:
<class name="Account" table="account" lazy="true">
<list name="entries" table="account_entry" inverse="true">
<key column="account_id"/>
<list-index column="entries_index"/>
<one-to-many class="Entry"/>
</list>
...
<class name="Entry" table="entry" lazy="true">
<many-to-one name="account" column="account_id" lazy="proxy" />
...
Here is the code that adds entries to an account:
Code:
// Add the entries in the transaction to the relevant accounts.
for (Entry entry : basicTransaction.getEntries())
{
Account account = accountDAO.retrieve(entry.getAccount().getId());
if (account == null)
{
throw new EntityValidationException("Orphaned entry in transaction not assigned to an account");
}
account.getEntries().add(entry);
}
What I end up with in the database after creating a transaction with 2 entries to 2 accounts, in the entries table:
Code:
id | account_id | amount | status_id | entries_index
----+------------+-----------+-----------+---------------
27 | 25 | -100.0000 | 2 | 1
28 | 26 | 100.0000 | 2 | 1
(2 rows)
I don't understand why this is happening, the entries should have entries_index = 0. In other places in my application this seems to work just fine, and list elements are numbered from 0.
The effect of the above, is that elsewhere in my application, when I retrieve the list of entries in an account, that list contains nulls, because when it is retrieved from the database, it is expected to start from 0.
Can anyone explain why this happens, and how to fix it?