如何在休眠中添加关键约束?
我有以下 *.hbm.xml 文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.models.results">
<class name="TestResult" table="testcaseresults">
<id name="testCaseResultsId" type="java.lang.Integer" column="testcaseresultsid">
<generator class="native"/>
</id>
<property name="start" type="java.sql.Timestamp" column="starttime"/>
<property name="end" type="java.sql.Timestamp" column="endtime"/>
<property name="results" type="java.lang.String" column="results"/>
<property name="passed" type="java.lang.Integer" column="passed"/>
<join table="testcase">
<key column="testcaseid"/>
<property name="testTitle" column="testcasename"/>
<property name="tfsid" column="tfsid"/>
</join>
<join table="selwowusers">
<key column="userid"/>
<property name="tester" column="useremail" type="java.lang.String"/>
</join>
</class>
现在,我的问题是“testcase”和“selwowusers”表具有外键约束(自身)。我到底如何编写一对多属性?我是休眠新手,在线教程的描述性还不够。
当我按原样运行该应用程序时,出现以下错误:
ERROR: HHH000388: Unsuccessful: alter table testcase add index FKBBAC26C2C073639B (testcaseid), add constraint FKBBAC26C2C073639B foreign key (testcaseid) references testcaseresults (testcaseresultsid)
Jan 23, 2012 4:08:57 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: Cannot add or update a child row: a foreign key constraint fails (`newseleniumwowmatrix`.`#sql-6379_4868`, CONSTRAINT `FKBBAC26C2C073639B` FOREIGN KEY (`testcaseid`) REFERENCES `testcaseresults` (`testcaseresultsid`))
Jan 23, 2012 4:08:57 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
谢谢。
I have the following *.hbm.xml file:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.models.results">
<class name="TestResult" table="testcaseresults">
<id name="testCaseResultsId" type="java.lang.Integer" column="testcaseresultsid">
<generator class="native"/>
</id>
<property name="start" type="java.sql.Timestamp" column="starttime"/>
<property name="end" type="java.sql.Timestamp" column="endtime"/>
<property name="results" type="java.lang.String" column="results"/>
<property name="passed" type="java.lang.Integer" column="passed"/>
<join table="testcase">
<key column="testcaseid"/>
<property name="testTitle" column="testcasename"/>
<property name="tfsid" column="tfsid"/>
</join>
<join table="selwowusers">
<key column="userid"/>
<property name="tester" column="useremail" type="java.lang.String"/>
</join>
</class>
Now, my problem is that the "testcase" and "selwowusers" tables have foreign key constrains (with themselves). How exactly do I write the one-to-many properties? I'm new to hibernate and the online tutorials haven't been too descriptive.
When I run the app as it is, I get the following errors:
ERROR: HHH000388: Unsuccessful: alter table testcase add index FKBBAC26C2C073639B (testcaseid), add constraint FKBBAC26C2C073639B foreign key (testcaseid) references testcaseresults (testcaseresultsid)
Jan 23, 2012 4:08:57 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: Cannot add or update a child row: a foreign key constraint fails (`newseleniumwowmatrix`.`#sql-6379_4868`, CONSTRAINT `FKBBAC26C2C073639B` FOREIGN KEY (`testcaseid`) REFERENCES `testcaseresults` (`testcaseresultsid`))
Jan 23, 2012 4:08:57 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Hibernate 允许您定义关系,但它不能强制执行约束。正如您可能观察到的,它是一个映射工具,用于将 java 对象与数据库表进行映射。它并不是为了定义数据模型。
-JavaJigsaw.com
Hibernate allows you to define relationships, it cannot enforce constraints. As you might observe, it is a mapping tool to map java objects with your database tables. It is not meant for defining data model.
-JavaJigsaw.com
一对多可以从 TestCase
<多对一名称=“testcaseID”类=“TestResult”列=“testcaseid”/>
The one-to-many can be represented as many-to-one from TestCase
<class name="Testcase" table="testcase">
<many-to-one name="testcaseID" class="TestResult" column="testcaseid"/>