2010-09-22 4 views
1

У меня есть наследование в Hibernate, где Connection - это моя родительская сущность, а MobilePhoneConnection - расширенная сущность. Я использовал одну таблицу для каждой стратегии подкласса для сопоставления наследования. Это мой файл:Наследование Hibernate и HQL

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

Мой вопрос, как я пишу HQL запрос на MobilePhoneConnection с WHERE проверки положение одного из наследуемых свойств (contractStartDate от подключения)? Я предполагаю, что мне нужны какие-то объединения, но не знаю, как это сделать? Я хочу, чтобы проверить погода MobilePhoneConnection дата начала контракта до или после того, как какой-то определенной даты ...

ответ

1

Я думаю, что это так же просто, как это:

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

выше запрос должен возвращать только MobilePhoneConnection экземпляры которых contractStartDate больше заданной даты (параметра). Спящий режим должен быть достаточно умным, чтобы выяснить инструкцию SQL, чтобы получить только записи, которые представляют собой MobilePhoneConnections.

0

Это то, что я на самом деле хотел и сумел получить его с помощью .class опции:

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

Это может быть полезно, когда один должен использовать псевдонимы, но кажется, что решение от Фредерика Gheysels работает. Что не работает для меня первый раз это:

from MobilePhoneConnection mb where mb.contractStartDate < current_date 

Было бы жаловаться, как это: 2010-09-22 10: 56: 19495 ERROR главный org.hibernate.hql.PARSER -: 1: 72: неожиданный узел AST: contractStartDate