【发布时间】:2012-01-18 15:02:14
【问题描述】:
我是 JPA 的新手,更不用说 Play 中的 JPA,所以我想知道将具有@ManyToOne 关系的对象保存到多个类的最佳方法是什么。
UserJump 类定义如下:
@Entity
public class UserJump extends Model{
@ManyToOne
public User user;
@ManyToOne
public JumpSession jumpSession;
public String parachuteUID;
public String notes;
public int status;
}
与 UserJump 的关系是从 JumpSession 内部定义的:
@OneToMany(mappedBy="jumpSession")
public List<UserJump> userJumps;
在 User 类中也是如此:
@OneToMany(mappedBy="user")
public List<UserJump> userJumps;
这是我现在保存东西的方式:
JumpSession jumpSession = new JumpSession();
//...Code here to fill in jumpSession...
jumpSession.save();
UserJump userJump;
for(String jumperUID : jumpers)
{
userJump = new UserJump();
userJump.jumpSession = jumpSession;
userJump.user = User.findById(jumperUID);
userJump.status = 1;
userJump.save();
}
在我看来,应该有一种方法可以将这些 UserJump 对象保存在名为 userJumps 的列表中,然后执行 jumpSession.userJumps = userJumps,然后执行 jumpSession.save(),它应该保留所有 UserJump 对象。有没有办法做到这一点?哪种方法是最好的方法?
【问题讨论】:
标签: mysql hibernate jpa persistence playframework