我正在使用休眠5.2.11。我有两个由netbeans hibernate插件4.3.1生成的类。这些类具有一对一的关系。我有一个查询,可以从两个类中获取数据。
我遇到的问题是此查询生成了正确的SQL(show_sql为true),该问题已在db上测试过,并且给出了正确的结果。但是休眠状态不会在第一个表中分配第二个表结果。
Transactions.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="some.package.Models.Transactions" optimistic-lock="version" table="TRANSACTIONS">
<id name="uuid" type="string">
<column length="40" name="UUID"/>
<generator class="assigned"/>
</id>
<many-to-one class="some.package.Models.User" fetch="select" name="user">
<column length="15" name="ID" not-null="true"></column>
</many-to-one>
<many-to-one class="some.package.Models.Service" fetch="select" name="service">
<column length="101" name="SERVICE_ID" not-null="true"></column>
</many-to-one>
<property name="isTran" type="char">
<column length="1" name="IS_TRAN" not-null="true"></column>
</property>
<property name="status" type="char">
<column length="1" name="STATUS" not-null="true"></column>
</property>
<property name="tranId" type="string">
<column length="101" name="TRAN_ID"></column>
</property>
<property name="added" type="timestamp">
<column name="ADDED" not-null="true"></column>
</property>
<one-to-one class="some.package.Models.MoneyTransaction" name="moneyTransaction" fetch="select"/>
</class>
</hibernate-mapping>
MoneyTransaction.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="some.package.Models.MoneyTransaction" optimistic-lock="version" table="MONEY_TRANSACTION">
<id name="uuid" type="string">
<column length="40" name="UUID"/>
<generator class="foreign">
<param name="property">transactions</param>
</generator>
</id>
<many-to-one class="some.package.Models.Payee" fetch="select" name="payee">
<column length="16" name="PAYEE_ID"></column>
</many-to-one>
<one-to-one class="some.package.Models.Transactions" constrained="true" name="transactions"/>
<property name="dir" type="char">
<column length="1" name="DIR" not-null="true"></column>
</property>
<property name="source" type="string">
<column length="15" name="SOURCE" not-null="true"></column>
</property>
<property name="dest" type="string">
<column length="15" name="DEST" not-null="true"></column>
</property>
<property name="amount" type="big_decimal">
<column name="AMOUNT" not-null="true" precision="22" scale="0"></column>
</property>
<property name="fee" type="big_decimal">
<column name="FEE" not-null="true" precision="22" scale="0"></column>
</property>
<property name="tranName" type="string">
<column length="100" name="TRAN_NAME"></column>
</property>
</class>
</hibernate-mapping>
查询
Select tran, tu.id from some.package.Models.Transactions tran join tran.user tu join tran.service ts left join fetch tran.moneyTransaction tmt left join fetch tmt.payee tp where tu.id = :id and tran.added >= :start and tran.added < :end order by tran.added desc
分配给对象
Transactions t = (Transactions) o[0];
String userId = (String) o[1];
它会生成正确的SQL,该SQL会提取moneyTransaction,但在运行查询并分配对象之后,moneyTransaction为null。为什么这样?此查询或代码有什么问题?