Hi, I am trying to map 3 tables, basically N:M where the join table has few columns to be mapped, so I need it to be 1:N and N:1 :
Code:
User <- 1:N -> Watches <- N:1 -> Topic
Here are the tables:
Code:
CREATE TABLE test_user (
id int(10) unsigned NOT NULL,
mail varchar(45) NOT NULL,
PRIMARY KEY (id)
)
CREATE TABLE test_watches (
id_user int(10) unsigned NOT NULL,
id_topic int(10) unsigned NOT NULL,
poznamka varchar(45) DEFAULT NULL,
PRIMARY KEY (id_user,id_topic)
)
CREATE TABLE test_topic (
id int(10) unsigned NOT NULL,
name varchar(45) NOT NULL,
PRIMARY KEY (id)
)
I have tried many approaches, but always got some Exception.
The Watches entity uses @EmbeddedId: (I've also tried @IdClass.)
Code:
@Entity
@Table( name = "test_watches" )
public class TestWatching implements Serializable
{
@EmbeddedId
protected TestWatchingPK testWatchingPK;
@Column( name = "poznamka" )
private String poznamka;
public TestWatching() { }
public TestWatching( TestWatchingPK testWatchingPK ) { this.testWatchingPK = testWatchingPK; }
public TestWatching( TestUser user, TestTopic topic ) {
this.testWatchingPK = new TestWatchingPK( user.getId(), topic.getId() );
}
// ... and getters, setters & overrides.
}
Code:
@Embeddable
public class TestWatchingPK implements Serializable
{
@Basic( optional = false )
@Column( name = "id_user" )
private int idUser;
@Basic( optional = false )
@Column( name = "id_topic" )
private int idTopic;
public TestWatchingPK() { }
public TestWatchingPK( int idUser, int idTopic ) {
this.idUser = idUser;
this.idTopic = idTopic;
}
// ... and getters, setters & overrides.
}
Then there's the User entity:
Code:
@Entity @Table( name = "test_user" )
public class TestUser implements Serializable
{
...
@OneToMany
@JoinTable(name="test_watches")
public Set<TestWatching> topicWatches = new HashSet();
...
}
This is the first relation I am trying to map and can't get over it. The above simple mapping says:
Code:
org.hibernate.AnnotationException: referencedColumnNames(id_user) of cz.dynawest.isir.entities.TestUser.topicWatches referencing cz.dynawest.isir.entities.TestWatching not mapped to a single property
I have also tried to play with @JoinColumn, with no success:
Code:
@OneToMany(/*mappedBy="idUser"*/)
@JoinTable(name="test_watches",
joinColumns={ @JoinColumn(name="id_user", referencedColumnName="id") }
)
public Set<TestWatching> topicWatches = new HashSet();
I've found a solution of similar problem, which uses XML mapping,
viewtopic.php?f=1&t=998512But when I use @JoinColumn with either name or referencedColumnName, Hibernate uses "" as a column name.
Perhaps adding an ID column to the test_watches table and converting the composite PK to an unique constraint could help, but changing the structure of the DB is the last option for me.
So please, is there any example of mapping one-to-many relation ship with FK being part of composite PK on the Many side? Is it possible at all?
I also had a look at Hibernate test-suite,
https://anonsvn.jboss.org/repos/hiberna ... tions/cid/ , but no such case there.
I use JPA annotations, but I am ok with any Hibernate-specific.
Thanks a lot,
Ondra