2016-11-28 8 views
0

私はmysqlに2つのテーブル、blocked_historiesblocked_detail_historiesを持っています。 block_detail_historiesテーブルは、block_historiesに対して多対1のハイバネート関係を持ちます。テーブル作成スクリプトこのHibernate HQL many to one join

blocked_histories

CREATE TABLE `blocked_histories` (
    `id` INT(11) NOT NULL AUTO_INCREMENT, 
    `type` VARCHAR(32) NOT NULL, 
    `no_ecash` varchar(13) NOT NULL, 
    `cif` VARCHAR(32) DEFAULT NULL, 
    `reason_id` INT(4) NOT NULL, 
    `desc` TEXT, 
    `status` VARCHAR(32) NOT NULL, 
    `created_date` DATETIME NOT NULL, 
    `created_by` VARCHAR(100) NOT NULL, 
    `total_balance` DECIMAL(15,6) NOT NULL, 
    `broker` int(11) NOT NULL, 
    `blocked_id` INT(11) NOT NULL UNIQUE, 
    `remark` text, 
    PRIMARY KEY (`id`), 
    CONSTRAINT `CUSTOMFIELD_ID_FK` FOREIGN KEY (`reason_id`) REFERENCES `custom_field_possible_values` (`id`) 
) ENGINE=INNODB AUTO_INCREMENT=312 DEFAULT CHARSET=latin1; 

blocked_detail_histories

CREATE TABLE `blocked_detail_histories` (
    `id` INT(11) NOT NULL AUTO_INCREMENT, 
    `blocked_id` INT(11) NOT NULL, 
    `no_ecash` VARCHAR(13) DEFAULT NULL, 
    `prev_group` INT(11) NOT NULL, 
    `balance` DECIMAL(15,6) NOT NULL, 
    PRIMARY KEY (`id`), 
    KEY `BLOCKED_ID_FK` (`blocked_id`), 
    CONSTRAINT `BLOCKED_ID_HISTORIES_FK` FOREIGN KEY (`blocked_id`) REFERENCES `blocked_histories` (`blocked_id`) 
) ENGINE=INNODB AUTO_INCREMENT=312 DEFAULT CHARSET=latin1; 

とHibernateのマッピングがこの

blocked_histories

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping> 

    <class dynamic-insert="true" dynamic-update="true" 
     name="nl.strohalm.cyclos.entities.groups.BlockedHistories" 
     table="blocked_histories"> 
     <id name="id" type="long"> 
      <column name="id" sql-type="bigint" /> 
      <generator class="native" /> 
     </id> 
     <property name="type" column="type" type="string" not-null="true" length="32" /> 
     <property name="noEcash" column="no_ecash" type="string" not-null="true" length="13" /> 
     <property name="cif" column="cif" type="string" length="32" /> 

     <!-- <property name="reasonId" column="reason_id" type="int" not-null="true" length="11" /> --> 
     <many-to-one name="reasonId" class="nl.strohalm.cyclos.entities.customization.fields.CustomFieldPossibleValue"> 
      <column name="reason_id" sql-type="integer"></column> 
     </many-to-one> 

     <property name="desc" column="desc" type="text" not-null="true" /> 
     <property name="status" column="status" type="string" length="32" /> 
     <property name="createdDate" column="created_date" type="calendar" not-null="true" /> 
     <property name="createdBy" column="created_by" type="string" not-null="true" length="100"/> 
     <property name="totalBalance" column="total_balance" type="big_decimal" not-null="true" precision="15" scale="6" /> 
     <property name="brokerId" column="broker" type="int" not-null="true" length="11" /> 
     <property name="remark" column="remark" type="text" /> 
     <property name="blockedId" column="blocked_id" type="int" not-null="true" length="11" /> 
    </class> 

</hibernate-mapping> 
のように見えるようになります。

blocked_detail_histories

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping> 

    <class dynamic-insert="true" dynamic-update="true" 
     name="nl.strohalm.cyclos.entities.groups.BlockedDetailHistories" 
     table="blocked_detail_histories"> 
     <id name="id" type="long"> 
      <column name="id" sql-type="bigint" /> 
      <generator class="native" /> 
     </id> 
     <many-to-one name="blockedHistories" class="nl.strohalm.cyclos.entities.groups.BlockedHistories"> 
      <column name="blocked_id" sql-type="integer"></column> 
     </many-to-one> 
     <property name="noEcash" column="no_ecash" type="string" not-null="true" length="13" /> 
     <property name="prevGroup" column="prev_group" type="int" not-null="true" length="11" /> 
     <property name="balance" column="balance" type="big_decimal" not-null="true" precision="15" scale="6" /> 
    </class> 

</hibernate-mapping> 

あなたが見ることができるように、blocked_detail履歴テーブルが

私が参加し、単純なMySQLのクエリを実行しようとした(「blockedHistories」欄で)blocked_historiesテーブルへ多対1の関係を持っていますID:上記のMySQLのクエリは正常に動作しますが、私はHQLクエリにmysqlのクエリの上に変換しようとしたとき、結果は...私には何も与えないHQLは、この(」のように見える

select bh.created_date, bdh.no_ecash, bh.type, 
    bh.cif, bh.desc, bh.reason_id, 
    bh.status, bh.created_by, bh.remark 
from blocked_detail_histories bdh 
join blocked_histories bh 
where bdh.blocked_id=4; 

このように、両方のテーブルに見えます" ここは JavaのNamedParameters)私はこの1つを試してみましたが、私がここで行方不明です何

select bh.createdDate, bdh.noEcash, bh.type, 
bh.cif, bh.desc, bh.reasonId, 
bh.status, bh.createdBy, bh.remark 
from BlockedDetailHistories bdh 
join bdh.blockedHistories bh 
where bh.blockedId = :id 

任意の提案あまりにも私には何も与えない

select bdh.blockedHistories.blockedId 
from BlockedDetailHistories bdh" 
join bdh.blockedHistories bh 
where bh.blockedId = :id 

?関係の定義はおそらく?それとも何か?任意のヘルプは、XMLの専門家は、マッピング、 を休止状態にない:)

+0

ジョインでSQLクエリが正しくないようです。 代わりに "どこで" 例の句 "ON" を使用する必要があります INNERがOrders.CustomerID = Customers.CustomerID ONお客様 のJOIN受注FROM 'SELECT Orders.OrderID、Customers.CustomerName、Orders.OrderDate 。 ' ソース:http://www.w3schools.com/sql/sql_join.asp –

答えて

0

を理解されるであろうが、あなたは怠け者の場合には(次のようにあなたがウル上記のクエリを再試行することができ、そのような定義された場合は、Hibernateのエンティティクラスの をフェッチ・タイプを適用していますフェッチタイプ)

select bdh.blockedHistories.blockedId 
from BlockedDetailHistories bdh 
left join fetch bdh.blockedHistories bh 
where bh.blockedId = :id