Hibernate use two foreign keys for different coloumns?

Is it possible to map two foreign keys associated with one table? its best i'll show you what i mean.

I have a table called fixtures, which currently looks like this:

<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> 

And I have another table called "Commands" that contains the data for the commands.

 <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> 

in a match / artifact, in each match there are two teams, a team away and at home, which I want to be displayed and attached to each other in a relationship with my team table. Is it possible in sleep mode? if so, how? What am I missing in my code?

Do they also have a way to say that each record of the device cannot have the same teams playing each other, i.e. away, and the home team should be unique?

welcomes in advance

+4
source share
1 answer

Is it possible to map two foreign keys associated with one table?

Yes. What you have should work. If not, please explain the problem.

Do they also have a way to say that each record of the device cannot have the same teams playing each other, i.e. away, and the home team should be unique?

This can be done using the properties element. From the documentation:

5.1.16. Properties

The <properties> element allows the definition of a named logical grouping of class properties. The most important use is that it allows the combination of properties as target of property-ref. It is also a convenient way to define a unique restriction on multiple columns .

See also this previous question for more details.


Update: I'm not sure why, but I could not get a unique key generated using the <properties> element (maybe I used it incorrectly, I did not try for too long), but here is a solution using the <natural-id> element .

Command Display:

 <?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> 

And fixture:

 <?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> 

And here is the generated 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 
+4
source

All Articles