Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
556 views
in Technique[技术] by (71.8m points)

join - Joining two unrelated tables in hibernate

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...