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>
对于有 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>
相关文章推荐
- hibernate中删除表遇到主键被外键引用违反完整约束条件不能删除的问题
- JAVA hibernate中删除表遇到主键被外键引用,违反完整约束条件,不能删除的问题
- Oracle12C--主外键约束删除数据问题--级联操作(九)
- 主外键约束问题,此列列表的唯一关键字或主键不匹配
- add constraint 已有字段添加外键约束,报错问题
- hibernate 外键关联 不生成外键约束
- Python之SQLAlchemy学习--外键约束问题
- 关于hibernate中双向外键关联one-to-one的property-ref=的问题
- 今天用Visual C#为客户做一个数据下载分析系统,碰到一个问题 未能启用约束。一行或多行中包含违反非空、唯一或外键约束的值。
- Oracle12C--主外键约束删除数据问题--级联操作(九)
- Oracle12C--主外键约束删除数据问题--级联操作(九)
- 如何解决DB2数据迁移中的外键约束问题
- DBUnit清空数据库遇到外键约束问题的解决方法
- hibernate级联更新外键 需要注意的问题
- SSH框架之Hibernate数据库外键如何插入值的问题
- LINQ to SQL 外键约束的插入及获取主表标识列等问题
- hibernate - 解决表结构外键限定为非空,级联保存和级联删除抛异常的问题
- Hibernate学习篇08——onetomany外键非空约束
- 由一个问题引发的思考——关于数据库的外键约束
- Oracle12C--主外键约束删除数据问题--级联操作(九)