如何在休眠中添加关键约束?

发布于 2024-12-28 13:39:44 字数 1944 浏览 0 评论 0原文

我有以下 *.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唯憾梦倾城 2025-01-04 13:39:44

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

像你 2025-01-04 13:39:44

一对多可以从 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"/>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文