Hibernate继承和HQL

发布于 2024-09-24 14:00:59 字数 5896 浏览 0 评论 0原文

我在 Hibernate 中有继承,其中 Connection 是我的父实体,MobilePhoneConnection 是扩展实体。我使用每个子类一张表的策略来进行继承映射。这是我的文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping schema="billing_cms" default-lazy="false" default-cascade="all">
    <class name="edu.etf.fuzzy.billingcms.domain.model.Connection"
        table="base_connection">
        <id name="ID" type="integer" column="id" access="field">
            <generator class="increment" />
        </id>
        <property name="contractStartDate" type="date" column="contract_start_date"
            not-null="true" />
        <property name="contractEndDate" type="date" column="contract_end_date" />
        <property name="contractNumber" type="string" column="contract_number" not-null="true" unique="true"/>
        <property name="description" type="string" column="description" />
        <property name="creationDate" not-null="true" type="date"
            column="date_created" />
        <property name="createdBy" type="string" column="created_by" />
        <property name="lastUpdatedBy" type="string" column="last_updated_by" />
        <property name="lastUpdateDate" type="date" column="last_modification_date" />
        <property name="isDeleted" not-null="true" type="boolean"
            column="deleted_indicator" />
        <property name="isFunctioning" type="boolean"
            column="functioning_indicator" />
        <property name="assignedUser" type="string" column="assigned_user" />

        <many-to-one name="administrativeCenter"
                     class="edu.etf.fuzzy.billingcms.domain.model.AdministrativeCenter"
                     column="administrative_center_id"/>

        <list name="bills" cascade="all">
            <key column="connection_id" />
            <index column="idx"></index>
            <one-to-many class="edu.etf.fuzzy.billingcms.domain.model.Bill" />
        </list>

        <joined-subclass name="edu.etf.fuzzy.billingcms.domain.model.MobilePhoneConnection"
                         table="mobile_phone_connection">
            <key column="id"/>
            <property name="operator" type="string" column="operator" not-null="true"/>
            <property name="userCode" type="string" column="user_code"/>
            <property name="packetName" type="string" column="packet_name" not-null="true"/>
            <property name="monthExpenseLimit" type="integer" column="month_expense_limit" not-null="true"/>
            <property name="isPrepaid" type="boolean" column="is_prepaid" not-null="true"/>
            <property name="lineNumber" type="string" column="line_number" not-null="true"/>
            <property name="hasGPRS" type="boolean" column="gprs"/>
            <property name="hasUMTS" type="boolean" column="umts"/>
            <property name="hasEDGE" type="boolean" column="edge"/>
        </joined-subclass>
        <joined-subclass name="edu.etf.fuzzy.billingcms.domain.model.PSTNConnection"
                         table="pstn_connection">
            <key column="id"/>
            <property name="operator" type="string" column="operator" not-null="true"/>
            <property name="userCode" type="string" column="user_code"/>
            <property name="lineNumber" type="string" column="line_number" not-null="true"/>
            <property name="monthExpenseLimit" type="integer" column="month_expense_limit"/>
            <property name="isLocalLoop" type="boolean" column="is_local_loop" not-null="true"/>
        </joined-subclass>
        <joined-subclass name="edu.etf.fuzzy.billingcms.domain.model.InternetConnection"
                         table="internet_connection">
            <key column="id"/>
            <property name="provider" type="string" column="provider" not-null="true"/>
            <property name="userCode" type="string" column="user_code"/>
            <property name="packetName" type="string" column="packet_name" not-null="true"/>
            <property name="linkThroughput" type="string" column="link_throughput" not-null="true"/>
            <property name="downloadCapacity" type="string" column="download_capacity" not-null="true"/>
            <property name="uploadCapacity" type="string" column="upload_capacity" not-null="true"/>
        </joined-subclass>
        <joined-subclass name="edu.etf.fuzzy.billingcms.domain.model.OuterConnection"
                         table="outer_connection">
            <key column="id"/>
            <property name="fromLocation" type="string" column="from_location" not-null="true"/>
            <property name="toLocation" type="string" column="to_location" not-null="true"/>
            <property name="userCode" type="string" column="user_code"/>
            <property name="lineNumber" type="string" column="line_number"/>
            <property name="monthExpenseLimit" type="integer" column="month_expense_limit" not-null="true"/>
            <property name="capacity" type="string" column="capacity"/>
        </joined-subclass>
    </class>

    <query name="getMobileConnectionByLineNumber">
        <![CDATA[from MobilePhoneConnection mb where mb.lineNumber = :lineNumber]]>
    </query>

</hibernate-mapping>

我的问题是如何在 MobilePhoneConnection 上编写 HQL 查询,并使用 WHERE 子句检查继承的属性之一(来自 Connection 的contractStartDate)?我猜我需要某种连接,但不确定如何实现这一点?我想检查天气 MobilePhoneConnection 合同开始日期是在某个特定日期之前还是之后...

I have inheritance in Hibernate for where Connection is my parent entity, and MobilePhoneConnection is extended entity. I used one table per subclass strategy for inheritance mapping. This is my file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping schema="billing_cms" default-lazy="false" default-cascade="all">
    <class name="edu.etf.fuzzy.billingcms.domain.model.Connection"
        table="base_connection">
        <id name="ID" type="integer" column="id" access="field">
            <generator class="increment" />
        </id>
        <property name="contractStartDate" type="date" column="contract_start_date"
            not-null="true" />
        <property name="contractEndDate" type="date" column="contract_end_date" />
        <property name="contractNumber" type="string" column="contract_number" not-null="true" unique="true"/>
        <property name="description" type="string" column="description" />
        <property name="creationDate" not-null="true" type="date"
            column="date_created" />
        <property name="createdBy" type="string" column="created_by" />
        <property name="lastUpdatedBy" type="string" column="last_updated_by" />
        <property name="lastUpdateDate" type="date" column="last_modification_date" />
        <property name="isDeleted" not-null="true" type="boolean"
            column="deleted_indicator" />
        <property name="isFunctioning" type="boolean"
            column="functioning_indicator" />
        <property name="assignedUser" type="string" column="assigned_user" />

        <many-to-one name="administrativeCenter"
                     class="edu.etf.fuzzy.billingcms.domain.model.AdministrativeCenter"
                     column="administrative_center_id"/>

        <list name="bills" cascade="all">
            <key column="connection_id" />
            <index column="idx"></index>
            <one-to-many class="edu.etf.fuzzy.billingcms.domain.model.Bill" />
        </list>

        <joined-subclass name="edu.etf.fuzzy.billingcms.domain.model.MobilePhoneConnection"
                         table="mobile_phone_connection">
            <key column="id"/>
            <property name="operator" type="string" column="operator" not-null="true"/>
            <property name="userCode" type="string" column="user_code"/>
            <property name="packetName" type="string" column="packet_name" not-null="true"/>
            <property name="monthExpenseLimit" type="integer" column="month_expense_limit" not-null="true"/>
            <property name="isPrepaid" type="boolean" column="is_prepaid" not-null="true"/>
            <property name="lineNumber" type="string" column="line_number" not-null="true"/>
            <property name="hasGPRS" type="boolean" column="gprs"/>
            <property name="hasUMTS" type="boolean" column="umts"/>
            <property name="hasEDGE" type="boolean" column="edge"/>
        </joined-subclass>
        <joined-subclass name="edu.etf.fuzzy.billingcms.domain.model.PSTNConnection"
                         table="pstn_connection">
            <key column="id"/>
            <property name="operator" type="string" column="operator" not-null="true"/>
            <property name="userCode" type="string" column="user_code"/>
            <property name="lineNumber" type="string" column="line_number" not-null="true"/>
            <property name="monthExpenseLimit" type="integer" column="month_expense_limit"/>
            <property name="isLocalLoop" type="boolean" column="is_local_loop" not-null="true"/>
        </joined-subclass>
        <joined-subclass name="edu.etf.fuzzy.billingcms.domain.model.InternetConnection"
                         table="internet_connection">
            <key column="id"/>
            <property name="provider" type="string" column="provider" not-null="true"/>
            <property name="userCode" type="string" column="user_code"/>
            <property name="packetName" type="string" column="packet_name" not-null="true"/>
            <property name="linkThroughput" type="string" column="link_throughput" not-null="true"/>
            <property name="downloadCapacity" type="string" column="download_capacity" not-null="true"/>
            <property name="uploadCapacity" type="string" column="upload_capacity" not-null="true"/>
        </joined-subclass>
        <joined-subclass name="edu.etf.fuzzy.billingcms.domain.model.OuterConnection"
                         table="outer_connection">
            <key column="id"/>
            <property name="fromLocation" type="string" column="from_location" not-null="true"/>
            <property name="toLocation" type="string" column="to_location" not-null="true"/>
            <property name="userCode" type="string" column="user_code"/>
            <property name="lineNumber" type="string" column="line_number"/>
            <property name="monthExpenseLimit" type="integer" column="month_expense_limit" not-null="true"/>
            <property name="capacity" type="string" column="capacity"/>
        </joined-subclass>
    </class>

    <query name="getMobileConnectionByLineNumber">
        <![CDATA[from MobilePhoneConnection mb where mb.lineNumber = :lineNumber]]>
    </query>

</hibernate-mapping>

My question is how do I write HQL query on MobilePhoneConnection with WHERE clause checking one of inherited properties (contractStartDate from Connection)? I am guessing I need some sort of joins but not sure how to accomplish this? I want to check weather MobilePhoneConnection contract start date is before or after some specific date...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伏妖词 2024-10-01 14:00:59

我认为它就像这样简单:

var query = session.CreateQuery ("from MobilePhoneConnection where contractStartDate > :p_date");

上面的查询应该只返回contractStartDate大于给定日期(参数)的MobilePhoneConnection实例。
Hibernate 应该足够聪明,能够找出 SQL 语句,以便只检索代表 MobilePhoneConnections 的记录。

I think it is just as simple as this:

var query = session.CreateQuery ("from MobilePhoneConnection where contractStartDate > :p_date");

The above query should only return MobilePhoneConnection instances whose contractStartDate is greater then the given date (parameter).
Hibernate should be smart enough to figure out the SQL statement so that only the records that represent MobilePhoneConnections are retrieved.

仙女山的月亮 2024-10-01 14:00:59

这就是我真正想要的,并设法通过使用 .class 选项来获得它:

from Connection c where c.contractStartDate < :afterdate and c.contractStartDate > :beforeDate and c.class = MobilePhoneConnection

当必须使用别名时,这可能很有用,但似乎 Frederik Gheysels 的解决方案也有效。第一次对我不起作用的是:

from MobilePhoneConnection mb where mb.contractStartDate < current_date

它会这样抱怨:
2010-09-22 10:56:19,495 错误主要 org.hibernate.hql.PARSER - :1:72:意外的 AST 节点:contractStartDate

This is what I actually wanted and managed to get it by using .class option:

from Connection c where c.contractStartDate < :afterdate and c.contractStartDate > :beforeDate and c.class = MobilePhoneConnection

This might be usefull when one have to use aliases, but seems like solution from Frederik Gheysels also works. What did not work for me first time is this:

from MobilePhoneConnection mb where mb.contractStartDate < current_date

It would complain like this:
2010-09-22 10:56:19,495 ERROR main org.hibernate.hql.PARSER - :1:72: unexpected AST node: contractStartDate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文