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.