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