JPA(休眠)映射OneToMany不正确?
你好 我是 JPA 新手,我认为我的映射注释有问题 我正在使用 hibernate 3.6.1 Final 和 JPA 2
以下是我的课程:
public class Resident {
...
@OneToMany(orphanRemoval = true, mappedBy = "resident")
@Cascade({org.hibernate.annotations.CascadeType.ALL})
public List<ResidentInfo> infos;
}
public class ResidentInfo {
...
@ManyToOne(optional = false)
public Resident resident;
}
当我第一次尝试保存数据时,一切都运行良好。
但是,当我尝试使用 save() 方法更新记录时,参数 orphanRemoval 似乎不适用。
例如,如果 infos 是 4 条记录的列表,并且我通过删除 2 条记录来更新它,则删除的 2 条记录不会从数据库中删除
我还有另一个问题,当我尝试将元素添加到列表 infos 时,然后我没有错误,但是添加的元素没有记录在数据库中。
为了记录数据,我只是使用 resident.save(),也许我错了?
Hi
I'm new to JPA and I think I have an issue with my mapping annotations
I'm using hibernate 3.6.1 final with JPA 2
Here are my class :
public class Resident {
...
@OneToMany(orphanRemoval = true, mappedBy = "resident")
@Cascade({org.hibernate.annotations.CascadeType.ALL})
public List<ResidentInfo> infos;
}
public class ResidentInfo {
...
@ManyToOne(optional = false)
public Resident resident;
}
When I try to save data for the first time, all is working perfectly.
However when I try to update a record by using save() method, the parameter orphanRemoval seems to don't be applied.
For instance if infos was a list of 4 records, and I update it by removing 2 records, the 2 records removed are not deleted from the database
I also have another issue, when I try to add an element to my list infos, then I don't have error, but the elements added are not recorded in the database.
In order to record datas I simply use resident.save(), maybe I'm wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你别忘了加上
添加到ManyToOne关系?
你是否使用类似的东西
和
Don't you forget to add
to the ManyToOne relationship ?
Are you using something like
and