2

I have an Account entity and an AccountTransaction entity.

Account 1 <----> n AccountTransaction

In my AccountTransaction.hbm.xml I specify a many-to-one relationship:

<hibernate-mapping>
<class name="com.walshk.accountmanager.domain.AccountTransaction" table="AccountTransaction">

    <id name="id" type="long" column="transaction_id">
        <generator class="increment"/>
    </id>

    <property name="date" not-null="true" type="date" column="transaction_date"/>

    <property name="description" not-null="true" column="transaction_description" length="500"/>

    <property name="amount" column="transaction_amount" not-null="true"/>

    <many-to-one name="account" column="account_id" not-null="true" cascade="all" lazy="false"/>

</class>
</hibernate-mapping>

This allows me to lookup AccountTransactions by account using

Criteria criteria = session.createCriteria(AccountTransaction.class)
    .add(Restrictions.eq("account", account));

and also allows me to get the Account instance using AccountTransaction#getAccount();

What I want to do now is provide a way to get an account, e.g

Criteria criteria = session.createCriteria(Account.class).add(Restrictions.eq("id", id));

But I also want the Account entity to have a method

List<AccountTransaction> getTransactions();

And I want this to be lazy loaded, since I may not even need to list the transactions.

Since I am already specifying the relationship as many-to-one from the AccountTransaction how do I now specify a one-to-many relationship giving me access from the other direction.

Additionally, what's the best way to handle lazy-loading, do I have to assign a session to each entity and not close the session? I could potentially have too many sessions open though.

Thanks.

3
  • Isn't lazy loading the default? Try to access the transactions outside of the session - you should get an Exception. Commented Feb 13, 2011 at 15:14
  • Yes it is. I just don't know how to specify the one-to-many relationship providing me a way of getting all transactions belonging to an account. And also I don't know how to avoid the Exception when they're loaded lazily. Commented Feb 13, 2011 at 15:24
  • P.s I can get the transactions easily enough, using the criteria and filtering by the account. Would be much more convenient to be able to get them from the Account instance itself, and less typing for me :) Commented Feb 13, 2011 at 15:25

2 Answers 2

2

If you add a One-to-Many association in your Account class hibernate mapping, you will get:

List<AccountTransaction> getTransactions();

from any ORM creation tool. One of the parameters of this association is the loading type - I am not familiar with the exact syntax in XML mapping, as we use annotations, but you could probably find it in any reference/documentation page of hibernate XML mapping.

Sign up to request clarification or add additional context in comments.

2 Comments

Makes sense, but there isn't a one-to-many xml tag that I can use in the mapping file. I'll have another look at the docs. Thanks.
Awesome I got it working using the <set> xml tag in the Account.hbm.xml file. Exactly what I was looking for :) Thanks very much.
1

in Order to work with Lazy Loading, You should have Open Session in view enabled. If you are using Spring integration that you have OpenSesionInViewIntereptor/OpenSessionInViewFilter

If you are using native Hibernate without Spring integration, then you can implement it by yourself. Please read the following:

http://community.jboss.org/wiki/OpenSessioninView

Hope it helps.

3 Comments

Thanks for the link. Helped loads regarding the lazy loading. I accepted your answer since it answers the second part of my question.
You are welcome. This is why I like Spring it allows me to work with Lazy-Loading using one line in config.
Spring's the next bit. Just want to make sure I understand the underlying hibernate bit and can at-least get something working before progressing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.