Hibernate 外键约束问题

本文探讨了在Hibernate配置中,1对多关系中外键约束的重要性。错误地设置inverse属性可能导致找不到主键的错误。举例说明了Machine和PositionMachine之间的关系,强调了inverse属性设置正确与否对数据更新控制的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值