有什么办法可以使两个表之间没有直接关系但又在hibernate中有两个公共字段呢?
我有两个表,称为boiler_plates和profile,它们之间没有直接关系,但是有一个名为contract_id的公共字段。
我写了查询“来自bp.bt_contracts = p.contract_id上的Boiler_Plates bp内部连接配置文件p”,但它不断抛出错误。“意外令牌:在第1行附近,第75列[来自com.catapult.bid.model.Boiler_Plates作为bp内部连接配置文件,在bp.bt_contracts = p.contract_id上为p,其中bp.bt_contracts = 1]。
以下是boiler_plates和配置文件的hibernate映射文件。
锅炉板的映射文件。
<?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>
在这种情况下,您唯一可以做的就是通过where子句进行内部联接:
select a from A a, B b where a.someColumn = b.someOtherColumn and ...
只有使用HQL中实体之间的关联,才能进行正确的联接。