2012-08-15 83 views
0

我是新来的休眠;使用旧版本使用.hbm.xml文件(无注释)的继承代码库休眠xml一对多连接三个表

我有一个表(比如表A),它与几个表(表B和C)具有一对多的休眠关系, “懒惰”属性设置为false;当我在做hiberateTemplate.load(Table a)时,它从所有三个表中获取数据。我的情况是我需要对一个子表(表B)进行联接,并在表B中查找特定的字段值,并仅从表B中的匹配字段值(表B)中获取来自所有A,B,C的记录特定领域)。

表A(比赛)

<set name="eventKeyIdentifiers" table="EventKeyIdentifier" 
      inverse="true" lazy="false" fetch="select"> 
     <key> 
      <column name="eventId" not-null="true" /> 
     </key> 
     <one-to-many class="event.EventKeyIdentifiers" /> 
    </set>  

    <set name="eventStatuses" table="EventStatus" 
      inverse="true" lazy="false" fetch="select" order-by="effectiveDate DESC"> 
     <key> 
      <column name="eventId" not-null="true" /> 
     </key> 
     <one-to-many class="event.EventStatuses" /> 
    </set>    

表B(事件状态)

<many-to-one name="event" class="event.Event" update="false" insert="false" fetch="select"> 
     <column name="eventId" length="36" not-null="true" /> 
    </many-to-one> 

    <property name="statusCode" type="string"> 
     <column name="statusCode" length="100" not-null="true" /> 
    </property> 

表A(比赛)需要加载特定的 “的StatusCode”(表B)

有什么建议吗?

+0

你将如何编写一个基本的sql查询来连接三个表?您可以编写一个类似于此的HQL查询。看看[HQL文档](http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#queryhql)寻求帮助。 – gresdiplitude 2012-08-16 07:43:10

回答

1

DetchedCriteria帮助!

 DetachedCriteria criteria = DetachedCriteria.forClass(Event.class) 
            .addOrder(Order.desc("eventProcessedDate")) 
            .createAlias("eventStatuses", "evtStats") 
            .add(Restrictions.naturalId() 
             .set("evtStats.statusCode", status));