Hello,
I have two persistent classes: Account and Transaction with the following hibernate mapping:
Code:
<class name="Account" table="account">
<id name="id" length="10">
<generator class="native"/>
</id>
<set name="transactions" table="transaction" inverse="true">
<key/>
<one-to-many class="Transaction"/>
<loader query-ref="transactions"/>
</set>
</class>
<class name="minibank.model.Transaction" table="transaction">
<id name="id" length="10">
<generator class="native"/>
</id>
<many-to-one name="from" column="`from`" class="Account" not-null="true"/>
<many-to-one name="to" column="`to`" class="Account" not-null="true"/>
</class>
<sql-query name="transactions">
<load-collection alias="tra" role="Account.transactions"/>
select {tra.*}
from transaction tra
where tra.from=:id or tra.to=:id
</sql-query>
That means, each transaction is related to two accounts ("from" and "to") and an account shall reference all transactions (via the set "transactions") that are related this account.
The problem is that this mapping does not work as expected. I'm wondering what hibernate does when I call account.getTransactions(). I thought it should be equivalent to this code:
Code:
List<Transaction> acc = session.getNamedQuery("transactions").setParameter("id", account.getId()).list();
But it's actually not! The second variant works and returns me the full list of related transaction whereby account.getTransactions() returns only a subset of those transactions.
Does someone know why it's not the same or what I'm doing wrong?