在休眠中加入两个不相关的表 [英] Joining two unrelated tables in hibernate

查看:133
本文介绍了在休眠中加入两个不相关的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法可以在两个表之间没有直接关系的情况下连接两个表,但在hibernate中有两个共同的字段?

我有两个名为boiler_plates和profile的表,它们之间没有直接关系,但有一个名为contract_id的公共字段。

我写了查询来自Boiler_Plates bp内部联接概要文件bp.bt_contracts = p.contract_id,但它一直在抛出错误。

意外标记:在第1行第75列[来自com.catapult.bid.model.Boiler_Plates作为bp内部联接概要文件作为p在bp.bt_contracts = p.contract_id其中bp.bt_contracts = 1]。

下面是boiler_plates和profiles的hibernate映射文件。

为boiler_plates映射文件。

 <?xml version = 1.0encoding =UTF-8?> 
<!DOCTYPE hibernate-mapping SYSTEMhttp://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">


< class name =Boiler_Platestable =bt_boiler_platesschema =bidtool>

< id name =boiler_plate_idtype =intcolumn =boiler_plate_id>
< generator class =sequence>
< param name =sequence> bidtool.boiler_plates_boiler_plate_id_seq< / param>
< / generator>
< / id>

< property name =boilerPlateNametype =string>
< column name =boiler_plate_namelength =20not-null =true/>
< / property>
< property name =editabletype =int>
< column name =editable/>
< / property>
< property name =boilerPlateContenttype =string>
< column name =boiler_plate_contentlength =20/>
< / property>
< property name =insertTimetype =dateinsert =false>
< column name =insert_time_stamp/>
< / property>
<多对一等级=合同fetch =selectname =bt_contracts>
< column name =contract_id/>
< /多对一>
< / class>
< / hibernate-mapping>

配置文件的映射文件

 <?xml version =1.0encoding =UTF-8?> 
<!DOCTYPE hibernate-mapping PUBLIC - // Hibernate / Hibernate Mapping DTD 3.0 // ENhttp://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">
< hibernate-mapping default-access =fieldpackage =com.catapult.bid.model>
< class name =Profilesschema =bidtooltable =bid_tool_profiles>
< id column =profile_idname =profileIdtype =string>
< generator class =com.catapult.bid.commons.ProfileIDGenerator/>
< / id>
<多对一等级=用户fetch =selectname =appUsers>
< column name =user_id/>
< /多对一>
< property name =profileContenttype =string>
< column name =profile_content/>
< / property>
< property name =scactype =string>
< column length =20name =scac/>
< / property>
< property name =createdtype =timestampupdate =false>
< column name =insert_timestamp/>
< / property>
< property name =statustype =string>
< column length =9name =statusnot-null =true/>
< / property>
< property name =editabletype =string>
< column length =1name =editablenot-null =true/>
< / property>
< / class>
< / hibernate-mapping>


解决方案

在这种情况下,您唯一能做的就是通过where子句做一个内连接:

 从a a,b b中选择一个a.someColumn = b.someOtherColumn和... 

只有使用HQL中的实体之间的关联才能进行适当的连接。


Is there any way that we can join two tables without direct relationship between them but have two common fields in hibernate?

I have two tables called boiler_plates and profile with no direct relationship between them but have a common field named contract_id.

I wrote the query "from Boiler_Plates bp inner join Profiles p on bp.bt_contracts=p.contract_id" but it keeps on throwing the error. " unexpected token: on near line 1, column 75 [from com.catapult.bid.model.Boiler_Plates as bp inner join Profiles as p on bp.bt_contracts=p.contract_id where bp.bt_contracts=1]".

Below are the hibernate mapping file for boiler_plates and profiles.

Mapping file for boiler_plates.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


<hibernate-mapping package="com.catapult.bid.model"  default-access="field" >
    <class name="Boiler_Plates" table="bt_boiler_plates" schema="bidtool">

        <id name="boiler_plate_id" type="int" column="boiler_plate_id">           
            <generator class="sequence">
                 <param name="sequence">bidtool.boiler_plates_boiler_plate_id_seq</param>
            </generator>
        </id>  

        <property name="boilerPlateName" type="string">
            <column name="boiler_plate_name" length="20" not-null="true" />
        </property>
        <property name="editable" type="int">
            <column name="editable"/>
        </property>
        <property name="boilerPlateContent" type="string">
            <column name="boiler_plate_content" length="20" />
        </property>
        <property name="insertTime" type="date" insert="false">
            <column name="insert_time_stamp"/>
        </property>
         <many-to-one class="Contracts" fetch="select" name="bt_contracts">
             <column name="contract_id"/>
        </many-to-one>
    </class> 
</hibernate-mapping>  

Mapping file for Profiles

  <?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 default-access="field" package="com.catapult.bid.model">
  <class name="Profiles" schema="bidtool" table="bid_tool_profiles">
    <id column="profile_id" name="profileId" type="string">
      <generator class="com.catapult.bid.commons.ProfileIDGenerator"/>
    </id>
    <many-to-one class="User" fetch="select" name="appUsers">
      <column name="user_id"/>
    </many-to-one>
    <property name="profileContent" type="string">
      <column name="profile_content"/>
    </property>
    <property name="scac" type="string">
      <column length="20" name="scac"/>
    </property>
    <property name="created" type="timestamp" update="false">
      <column name="insert_timestamp"/>
    </property>
    <property name="status" type="string">
      <column length="9" name="status" not-null="true"/>
    </property>
    <property name="editable" type="string">
      <column length="1" name="editable" not-null="true"/>
    </property>
  </class>
</hibernate-mapping>

解决方案

The only thing you can do in such a case is to do an inner join via the where clause:

select a from A a, B b where a.someColumn = b.someOtherColumn and ...

Proper joins are only possible using associations between entities in HQL.

这篇关于在休眠中加入两个不相关的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆