【问题标题】:Joining two unrelated tables in hibernate在休眠中加入两个不相关的表
【发布时间】:2012-05-25 16:32:21
【问题描述】:

有什么方法可以连接两个没有直接关系但在hibernate中有两个公共字段的表?

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

我编写了查询“来自 Boiler_Plates bp 内部连接 ​​Profiles p on bp.bt_contracts=p.contract_id”,但它一直抛出错误。 " 意外令牌:在第 1 行附近,第 75 列 [来自 com.catapult.bid.model.Boiler_Plates 作为 bp 内部连接 ​​Profiles as p on bp.bt_contracts=p.contract_id where bp.bt_contracts=1]”。

以下是boiler_plates 和profiles 的休眠映射文件。

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>  

配置文件的映射文件

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

【问题讨论】:

    标签: hibernate join


    【解决方案1】:

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

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

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

    【讨论】:

    • 但这会导致生成的SQL出现CROSS JOIN!
    【解决方案2】:

    您需要从 Contracts 中延迟获取 Boiler_Plates(您的类的名称中不应包含下划线)和 Profiles。然后使用类似的东西:

    select bp.contracts.profiles from Boiler_Plates bp where bp.boiler_plate_id=?
    

    您的代码使用了太多下划线,您也应该查看 java 编码约定:http://www.oracle.com/technetwork/java/codeconv-138413.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      • 2019-02-21
      • 2019-05-23
      • 2017-05-26
      • 2019-05-24
      • 1970-01-01
      • 2013-06-04
      相关资源
      最近更新 更多