Hi
I have the two entity classes, User and MyCharacter. User has a list of MyCharacters and each MyCharacter has a reference back to the User (owner). What I'd like to accomplish is, that I use the same join table for both relations, meaning, that the owner relation found in MyCharacter would automatically use the same join table as from User=>MyCharacter. This means that the getOwner() method in MyCharacter should work without me having to explicitly at some point call setOwner(user).
To clear this a bit more, here's my unit test which currently fails (last assert fails)
@Test
public void testTwoWayRelation() {
User user = new User();
MyCharacter character = new MyCharacter();
List<MyCharacter> chars = new ArrayList<MyCharacter>();
chars.add(character);
user.setCharacters(chars);
facade.store(user);
assertNotNull(character.getId());
character = facade.find(MyCharacter.class, character.getId());
assertNotNull(character.getOwner());
}
My entity classes are listed below.
@Entity
@Table(name = "myuser")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Long id;
@OneToMany(cascade = { CascadeType.PERSIST })
protected List<MyCharacter> characters;
public User() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<MyCharacter> getCharacters() {
return characters;
}
public void setCharacters(List<MyCharacter> characters) {
this.characters = characters;
}
}
@Entity
public class MyCharacter{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Long id;
@ManyToOne
@JoinTable(name = "myuser_mycharacter", joinColumns = @JoinColumn(name = "characters_id"), inverseJoinColumns = { @JoinColumn(name = "user_id") })
protected User owner;
public MyCharacter() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public User getOwner() {
return owner;
}
public void setOwner(User owner) {
this.owner = owner;
}
}