4

is it possible to map two foreign keys linked to the same table? its best i 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 Teams which holds the teams data.

<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/fixture, each match has two teams, an away and home team that i want it to map and linked in a one to many relaltionship with my teams table. is that possible in hibernate? if so, how? what am i missing in my code?

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?

cheers in advance

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Jono
  • 17,341
  • 48
  • 135
  • 217
  • What's the problem with your current mapping? – Pascal Thivent Nov 03 '10 at 17:05
  • that will work? im away from my IDE and just programmed it on paper mate. also, no sign of how i can make each fixture record have unique away and home team id's inputed to each record or else it may be possible to add two teams of the same name playing eachother! – Jono Nov 03 '10 at 17:09

1 Answers1

4

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:

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.

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
Community
  • 1
  • 1
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • yea i have checked out the properties link and its not quite what im after. it makes a specific column have a unique value BUT i dont want that, i want the away and home team column to be unique against eachother ie they both cant have same team names on a record – Jono Nov 03 '10 at 17:59
  • edit: now read your second link. not quite clear. how is creating a properties tag with the two propertie columns inside able to map it towards a pojo of fixture that will only contain getters and setters for away and home team? do i need to create some kind of seperate pojo that holds both away and home teams in order to map it correcntly with the hbm file? for example create a pojo called uk1 and another one called uk2 given the example you have posted? – Jono Nov 03 '10 at 18:21
  • @jonney: Hmm... The `` element allows to *group properties* and to define a unique constraint on them, it's exactly what you're looking for. I don't understand what is not clear but I'll post a sample mapping. – Pascal Thivent Nov 03 '10 at 18:31
  • But how is that group mapped in a pojo or it doesnt need to be mapped? – Jono Nov 03 '10 at 18:40
  • @jonney: The group itself doesn't need to be mapped, the properties inside it are. That said, I couldn't get the expected result (maybe I misused the element though) and tried an alternative approach. – Pascal Thivent Nov 03 '10 at 19:35
  • cheers pal i will try this out later today. thanks for the time in helping me out. much appreciate it – Jono Nov 04 '10 at 06:50
  • hi mate, is their a online generated DDL tool i can use where i type in my hibernate mappings and the website tool generated the above generated DDL? – Jono Nov 05 '10 at 06:49
  • @jonney: That could be nice but I'm not aware of such a tool. – Pascal Thivent Nov 06 '10 at 09:26
  • ahh ok cool. i still have not tried your code mate but will do soon. cheers for your help though. – Jono Nov 08 '10 at 14:23