简体   繁体   English

Hibernate HQL多对一连接

[英]Hibernate HQL many to one join

i have 2 tables in mysql, blocked_histories and blocked_detail_histories . 我在mysql中有2个表, blocked_historiesblocked_detail_histories The block_detail_histories table has many-to-one hibernate relationship to block_histories. block_detail_histories表与block_histories具有多对一的休眠关系。 the table creation scripts looks like this 表创建脚本如下所示

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 block_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;

and the hibernate mapping looks like this 休眠映射看起来像这样

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

as you can see, the blocked_detail histories table has many-to-one relationship to blocked_histories table (in "blockedHistories" column) 如您所见,blocked_detail histories表与blocked_histories表具有多对一关系(在“ blockedHistories”列中)

i tried to run a simple mysql query to join both tables that looks like this 我试图运行一个简单的mysql查询来连接看起来像这样的两个表

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;

the mysql query above works fine, but when i tried to convert above mysql query to HQL query, the result gives me nothing.. the HQL looks like this (the ":id" here is java NamedParameters) 上面的mysql查询工作正常,但是当我尝试将上面的mysql查询转换为HQL查询时,结果什么也没有给我。HQL看起来像这样(“:id”在这里是Java NamedParameters)

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

i tried this one, but gives me nothing too 我尝试了这个,但是也没有给我任何东西

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

any suggestions what i am missing here? 任何建议,我在这里想念什么? relationship definition maybe? 关系定义呢? or anything else? 还是其他? any help will be appreciated :) 任何帮助将不胜感激 :)

Not an expert in xml hibernate mapping, but have you applied fetch type for hibernate entity class, if you defined such then you can retry ur above query as follows (in case of lazy fetch type) 不是xml休眠映射的专家,但是您是否为休眠实体类应用了提取类型,如果您定义了这种类型,则可以按以下方式在查询中重试(对于延迟提取类型)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM