【问题标题】:hibernate xml one-to-many JOIN three tableshibernate xml 一对多 JOIN 三张表
【发布时间】:2012-08-15 16:17:02
【问题描述】:

我是休眠新手;继承的代码库使用使用 .hbm.xml 文件的旧版本(无注释)

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

表 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>

需要为特定的“statusCode”(表 B)加载表 A(事件)

有什么建议吗?

【问题讨论】:

  • 如何编写基本的 sql 查询来连接三个表?您可以编写类似的 HQL 查询。查看HQL documentation 寻求帮助。

标签: xml hibernate one-to-many


【解决方案1】:

DetchedCriteria 有帮助!

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 2018-01-11
    • 2016-12-09
    • 2014-10-31
    • 2015-08-23
    • 1970-01-01
    相关资源
    最近更新 更多