gpt4 book ai didi

java - hibernate 对不同的列使用两个外键?

转载 作者:搜寻专家 更新时间:2023-11-01 02:54:38 26 4
gpt4 key购买 nike

是否可以映射链接到同一个表的两个外键?我最好向你展示我的意思。

我有一个名为 fixtures 的表,目前看起来像这样:

<class name="" table="">
<id name="id" column="id">
<generator class="Increment" />
</id>
<property name="date" type="" column="" />
<property name="" type="" column="" />
<many-to-one name="awayTeam" column="teamId" not-null="true" />
<many-to-one name="homeTeam" column="teamId" not-null="true" />
</class>

我还有另一个名为 Teams 的表,其中包含团队数据。

<class name="" table="">
<id name="teamId" column="id">
<generator class="Increment" />
</id>
<property name="name" type="String" column="name" />
<property name="fixturesUrl" type="String" column="fixturesUrl" />
<property name="rssNewsUrl" type="String" column="rssNewsUrl" />
</class>

在一场比赛/比赛中,每场比赛都有两支球队,一支客场球队和一支主场球队,我希望它能够映射并与我的球队表建立一对多关系。这在 hibernate 中可能吗?如果是这样,如何?我的代码中缺少什么?

他们是否也表示每个赛程记录不能让相同的球队互相比赛,即客场和主队必须是唯一的?

提前加油

最佳答案

is it possible to map two foreign keys linked to the same table?

是的。你有什么应该工作。如果不是,请说明问题。

Is their also a way to say each fixture record cannot have same teams playing eachother i.e. away and home team has to be unique?

使用 properties 应该是可行的元素。来自文档:

5.1.16. Properties

The <properties> element allows the definition of a named, logical grouping of the properties of a class. The most important use of the construct is that it allows a combination of properties to be the target of a property-ref. It is also a convenient way to define a multi-column unique constraint.

另见 previous question了解更多详情。


更新:我不确定为什么,但我无法获得使用 <properties> 生成的唯一 key 元素(也许我误用了它,我没有尝试太久)但这是一个使用 <natural-id> 的解决方案元素。

团队映射:

<?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.stackoverflow.q4089539.Team" table="TEAM">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="fixturesUrl" type="java.lang.String">
<column name="FIXTURESURL" />
</property>
<property name="rssNewsUrl" type="java.lang.String">
<column name="RSSNEWSURL" />
</property>
</class>
</hibernate-mapping>

和夹具:

<?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.stackoverflow.q4089539.Fixture" table="FIXTURE">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="native" />
</id>
<natural-id mutable="false">
<many-to-one class="com.stackoverflow.q4089539.Team" name="awayTeam" not-null="true">
<column name="AWAYTEAM"/>
</many-to-one>
<many-to-one class="com.stackoverflow.q4089539.Team" name="homeTeam" not-null="true">
<column name="HOMETEAM" />
</many-to-one>
</natural-id>
<property generated="never" lazy="false" name="date" type="java.util.Date">
<column name="DATE" />
</property>
</class>
</hibernate-mapping>

这是生成的 DDL:

create table FIXTURE (
ID bigint not null,
AWAYTEAM bigint not null,
HOMETEAM bigint not null,
DATE timestamp,
primary key (ID),
unique (AWAYTEAM, HOMETEAM)
)

create table TEAM (
ID bigint not null,
NAME varchar(255),
FIXTURESURL varchar(255),
RSSNEWSURL varchar(255),
primary key (ID)
)

alter table FIXTURE
add constraint FKF88585E9445D9A98
foreign key (AWAYTEAM)
references TEAM

alter table FIXTURE
add constraint FKF88585E987B44C09
foreign key (HOMETEAM)
references TEAM

关于java - hibernate 对不同的列使用两个外键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4089539/

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