is it possible to map two foreign keys linked to the same table?
Yes. What you have should work. If it doesn't, please explain the problem.
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?
This should be doable using the properties
element. From the documentation:
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.
See also this previous question for more details.
Update: I'm not sure why but I couldn't get the unique key generated using the <properties>
element (maybe I misused it, I didn't try too long) but here is a solution using a <natural-id>
element.
The mapping of Team:
<?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 the 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