I'm creating a sports related schema and have 3 entities: Sport, Team and Player.
The DB primary key for for sport is sport_id while both Team and Player have composite primary keys consisting of sport_id (a foreign key to Sport obviously) and team_id or player_id.
Sport and Team are fine but I'm running into an issue with Player which has foreign keys to BOTH Sport and Team.... seems trivial but I can't find an answer on the forums
@Entity
public class Sport {
@Id
private int sportId;
...
}
@Entity
public class Team {
@Id
@ManyToOne
@JoinColumn(name="sport_id")
private Sport sport;
@Id
private int teamId;
...
}
@Entity
public class Player {
@Id
@ManyToOne
@JoinColumn(name="sport_id")
private Sport sport;
@Id
private int playerId;
// not part of ID / pkey
// hibernate NO LIKEY this relationshi
@ManyToOne
@JoinColumns({
@JoinColumn(name="sport_id"),
@JoinColumn(name="team_id)
})
private Team team;
...
}
the ManyToOne from Player to Team yields this error however:
A Foreign key refering com.espn.composer.contentlink.entity.Team from com.espn.composer.contentlink.entity.Player has the wrong number of column. should be 1
See the full trace below... what am doing wrong? This seems like it should be pretty easy :-)
Hibernate version: 3.2.6
Mapping documents: n/a
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs: Caused by: javax.persistence.PersistenceException: org.hibernate.AnnotationException: A Foreign key refering com.espn.composer.contentlink.entity.Team from com.espn.composer.contentlink.entity.Player has the wrong number of column. should be 1
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:247)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:120)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)
at com.espn.composer.contentlink.test.PlayerTest.<clinit>(PlayerTest.java:12)
Caused by: org.hibernate.AnnotationException: A Foreign key refering com.espn.composer.contentlink.entity.Team from com.espn.composer.contentlink.entity.Player has the wrong number of column. should be 1
at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:255)
at org.hibernate.cfg.FkSecondPass.doSecondPass(FkSecondPass.java:64)
at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:428)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:286)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1121)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1211)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:154)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:847)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:178)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:235)
... 4 more
|