您的位置:首页 > 其它

Hibernate 外键约束问题

2007-07-26 22:03 239 查看
Hibernate 配置中,

对于有 1 对多的外键约束关系, 如果把inverse 属性设置反了的话 ,

就有可能找不到 出现找不到主键的错误

比如一太机器对应多个  岗位的问题

Machine.hbm.xml 如下

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="com.novemsoft.model.Machine" table="CMM_MCHN" lazy="false">
        <id name="id" type="integer">
            <column name="MCHN_KEY" />
            <generator class="increment" />
        </id>
        <property name="name" type="string">
            <column name="MCHN_NAME" length="30" />
        </property>
        <property name="enname" type="string">
            <column name="MCHN_ENG_NAME" length="30" />
        </property>
        <property name="code" type="string">
            <column name="MCHN_CODE " length="30" />
        </property>
        <!-- 
        <property name="depreciationValue" type="double">
            <column name="depreciation_value" precision="22" scale="0" />
        </property>
        -->
        <property name="beginDate" type="timestamp">
            <column name="MCHN_BGN_DATE" length="19" />
        </property>
        <property name="endDate" type="timestamp">
            <column name="MCHN_END_DATE" length="19" />
        </property>
        <property name="status" type="int">
            <column name="MCHN_STS_CODE"/>
        </property>
       
        <set name="cmm_pstn_mchn" inverse="true" cascade="all" lazy="false">
         <key column="MCHN_KEY"></key>
         <one-to-many class="com.novemsoft.model.PositionMachine"/>
        </set>
       
        <!--  here if we set the inverse = false, than we can't update the positionMachine table when

 we use session.saveOrUpdate(Machine);  in other words here inverse =true mean let the machine side to control the update , if the inverse=false  let  the positionMachine side to control update

 

   -->
    </class>
</hibernate-mapping>

PositionMachine.hbm.xml  as following

<?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 name="com.novemsoft.model.PositionMachine" table="cmm_pstn_mchn">
  <id name="id" type="integer">
            <column name="PSTN_MCHN_KEY" />
            <generator class="increment" />
        </id>
       
        <many-to-one name="cmm_mchn"  class="com.novemsoft.model.Machine" column="MCHN_KEY" lazy="false">
        </many-to-one>
       
        <many-to-one name="cmm_pstn"  class="com.novemsoft.model.Position" column="PSTN_KEY" lazy="false">
        </many-to-one>
       
        <!-- 
        <property name="positionId" type="integer">
         <column name="PSTN_KEY" not-null="true"/>
        </property>
        <property name="positionName" type="string">
         <column name="position_name" length="30"/>
        </property>
        <property name="machineId" type="integer">
         <column  name="machine_id" not-null="true"/>
        </property>
        <property name="machineName" type="string">
         <column name="machine_name" length="30"/>
        </property>
        <property name="machineCode" type="string">
         <column name="machine_code" length="30"/>
        </property>
        -->
 </class>
</hibernate-mapping>

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息