我有一个查询,当我在sqlDeveloper中运行时,我得到了结果。这是查询
select * from Losa_Cust_Reject_App rejectApp
inner join
Losa_Cust losaCust
on
rejectApp.app_Ref_No = losaCust.app_Ref_No
where
rejectApp.app_Ref_No != '0000001912' and rejectApp.app_Dt >= '23-SEP-2012'
and
rejectApp.cust_Id1 = 'A111111111' and rejectApp.cust_Id1_Type = '01';
我将此查询翻译为hibernate,如下所示
public List findAppByDate(String appRefNo, Date previousAppDate, String currentId1,
String currentIdType1) {
StringBuffer query = new StringBuffer("from ");
query.append(LosaCustRejectApp.class.getName());
query.append(" rejectApp inner join ");
query.append(LosaCust.class.getName());
query.append(" losaCust with rejectApp.appRefNo = losaCust.comp_id.appRefNo");
query.append(" where rejectApp.appRefNo != ? and rejectApp.appDt >= ?");
query.append(" and rejectApp.custId1 = ? and rejectApp.custId1Type = ? ");
List result = null;
try {
result = getHibernateTemplate().find(query.toString(),
new Object[] { appRefNo, previousAppDate, currentId1, currentIdType1 });
if (CollectionUtils.isNotEmpty(result)) {
return result;
}
} catch (Exception e) {
String message = e.getMessage();
System.out.println();
}
return result;
}
但是我想在hibernate中定义映射,所以hibernate会自动进行连接。如何在hibernate文件中为上述查询定义关联,以便hibernate自动进行连接。
这是我的LosaCustReject.hbm.xml
<hibernate-mapping default-lazy="false">
<class name="com.thetasp.losa.data.LosaCustRejectApp" table="LOSA_CUST_REJECT_APP" optimistic-lock="version">
<id name="rejectAppId" type="java.lang.Long" column="REJECT_APP_ID">
<generator class="com.thetasp.code.runningno.HibernateId">
<param name="fieldCode">REJECT_APP_ID</param>
<param name="returnType">java.lang.Long</param>
</generator>
</id>
<version name="rowVersion" type="int" column="ROW_VERSION" access="property" unsaved-value="undefined" />
<property name="appRefNo" type="java.lang.String" column="APP_REF_NO" not-null="true" length="20" />
<property name="appDt" type="java.sql.Timestamp" column="APP_DT" not-null="true" length="23" />
<property name="custId1Type" type="java.lang.String" column="CUST_ID1_TYPE" not-null="true" length="1" />
<property name="custId1" type="java.lang.String" column="CUST_ID1" not-null="true" length="20" />
.......
<property name="updateDt" type="java.sql.Timestamp" column="UPDATE_DT" length="23" />
<!-- associations -->
</class>
</hibernate-mapping>
LosaCust.hbm.xml
<hibernate-mapping default-lazy="false">
<class name="com.thetasp.losa.data.LosaCust" table="LOSA_CUST" discriminator-value="I" optimistic-lock="version">
<composite-id name="comp_id" class="com.thetasp.losa.data.LosaCustPK">
<key-property name="custId" column="CUST_ID" type="java.lang.String" length="20" />
<key-property name="appRefNo" column="APP_REF_NO" type="java.lang.String" length="20" />
</composite-id>
<discriminator column="CUST_T" type="string" length="1" >
</discriminator>
<version name="rowVersion" type="int" column="ROW_VERSION" access="property" unsaved-value="undefined" />
<property name="custLevel" type="java.lang.String" column="CUST_LEVEL" not-null="false" length="10" />
.....
<property name="staySinceMth" type="java.lang.String" column="STAYED_SINCE_MTH" length="10" />
....
<subclass name="com.thetasp.losa.data.LosaIndvCust" discriminator-value="I" lazy="false" >
<property name="id1Type" type="java.lang.String" column="ID1_TYPE" length="1" />
<property name="id1" type="java.lang.String" column="ID1" length="20" />
....
</subclass>
</class>
谢谢
答案 0 :(得分:0)
我认为你可以做这样的事情
在LosaCust.java中定义一个字段,表示与LosaCustRejectApp实体的一对多关联:
List<LosaCustRejectApp> associatedLosaCustRejectApps;
在LosaCust.hbm.xml中,您可以使用以下内容映射此新字段:
<list name="associatedLosaCustRejectApps" table="LOSA_CUST_REJECT_APP" >
<key>
<column name="app_Ref_No" not-null="true" />
</key>
<one-to-many class="com.thetasp.losa.data.LosaCustRejectApp" />
</set>
有关更多详细信息,甚至是关于如何进行此类映射的完整教程,您可以访问:
Hibernate – One-to-Many example (XML Mapping)
祝你好运