我有两个实体类,User和MyCharacter。用户有一个MyCharacters列表,每个MyCharacter都有一个返回给用户(所有者)的引用。我想要完成的是,我对两个关系都使用相同的连接表,这意味着在MyCharacter中找到的所有者关系将自动使用与User => MyCharacter相同的连接表。这意味着MyCharacter中的getOwner()方法应该可以工作,而不必在某些时候显式调用setOwner(user)。
为了清楚这一点,这是我目前失败的单元测试(最后断言失败)
@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());
}
我的实体类列在下面。
@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;
}
}
答案 0 :(得分:2)
这就是我们如何在项目中使用jpa加入两个实体:
@Entity
@Table(name = "Period")
public class Period implements Serializable {
private List<Delay> delays = new ArrayList<Delay>();
@OneToMany(mappedBy="period") //name of the field in joined entity
public List<Delay> getDelays() {
return delays;
}
}
@Entity
@Table(name = "Delay")
public class Delay implements Serializable {
private Period period;
@ManyToOne
@JoinColumn(name = "PERIODID")
public Period getPeriod() {
return period;
}
}
答案 1 :(得分:1)
我不确定我是否正确理解了您的问题,但您可以尝试在MyCharacter.owner上设置mappedBy:
@ManyToOne(mappedBy="characters")
答案 2 :(得分:0)
Here is an article解释了如何映射这种关系。
在Java中使用它时,根据您的JPA框架,您需要将MyCharacter
添加到User
中的列表中并设置{{1} user
中的}字段或两者中只有一个(因为框架将为您管理另一方)。我建议写一个小测试来弄清楚什么有用(并且你应该为你使用对象的所有方式编写测试用例)。
从数据库加载对象时,您不需要这样做,因为所有框架都能正确处理这种情况。