gpt4 book ai didi

java - Hibernate OneToMany,天然PK

转载 作者:行者123 更新时间:2023-12-02 03:25:49 24 4
gpt4 key购买 nike

我在设置 OneToMany 关系时遇到问题。

我的数据库架构:

TABLE PARENT (
VENDOR,
CHANNEL,
CREATEDATE,
REFID,
...
)
UNIQUE INDEX PK_PARENT (VENDOR, CHANNEL, CREATEDATE, REFID)

TABLE CHILD (
REFID,
NAME,
)
UNIQUE INDEX PK_CHILD (REFID, NAME)

和Java类

public class Parent {
String vendor;
String channel;
Date createdate;
Long refid;
List<Child> childs;
...
}

public class Child {
Long refid;
String name;
}

我的问题是我无法为此类开发正确的映射。出于一致的原因,我被迫使用 hbm xml。我能够将其映射为两个单独的实体。

<hibernate-mapping>
<class name="Parent" table="PARENT">
<composite-id >
<key-property name="vendor" type="string">
<column name="VENDOR" />
</key-property>
<key-property name="vendor" type="string">
<column name="CHANNEL" />
</key-property>
<key-property name="vendor" type="Date">
<column name="CREATEDATE" />
</key-property>
<key-property name="vendor" type="Long">
<column name="REFID" />
</key-property>
<composite-id />
<list name="childs" table="CHILD" lazy="true" fetch="select">
<key column="REFID" />
<index column="REFID" />
<one-to-many class="Child" />
</list>
</class>

<class name="Child" table="CHILD">
<composite-id >
<key-property name="vendor" type="Long">
<column name="REFID" />
</key-property>
<key-property name="name" type="string">
<column name="NAME" />
</key-property>
<composite-id />
</class>
</hibernate-mapping>

我遇到异常:

Caused by: org.hibernate.MappingException: Foreign key (FK_9897tr114hxkewktsi9vb92x6:CHILD[REFID])) must have same number of columns as the referenced primary key (PARENT[VENDOR,CHANNEL,CREATEDATE,REFID]) 
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:110)
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:93)
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1818)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1741)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1426)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)

对此有什么想法吗?

最佳答案

因为您的子实体有一个复杂的键,因此在父实体中您也需要将外键指定为复杂。

关于java - Hibernate OneToMany,天然PK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38912410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com