-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: List being stored with first index = 1, should be = 0.
PostPosted: Mon Jun 08, 2015 5:50 am 
Newbie

Joined: Wed Aug 16, 2006 5:52 pm
Posts: 5
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?


Top
 Profile  
 
 Post subject: Re: List being stored with first index = 1, should be = 0.
PostPosted: Mon Jun 08, 2015 6:23 am 
Newbie

Joined: Wed Aug 16, 2006 5:52 pm
Posts: 5
Before the code where the entries are added to the account, I had updated the account balance, and saved the account with an empty list of entries.

It would seem that doing:

Code:
account.setEntries(new LinkedList());
session.save(account);

account.getEntries().add(entry);
session.save(account);


Will result in the entries being indexed from 1. Perhaps this is a bug that could be reduced to a SSCCE. It was easy enough to work around by making my code more sensible.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.