如何使用 hibernate 持久保存 @OneToMany 更新?
在我的应用程序中,我有拥有文件的用户。 UserVO 具有以下映射:
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "users")
public Set<Files> getFiles()
{
return this.files;
}
然后我有一个 HibernateDAO ,具有以下 更新 方法:
public void update(T vo)
{
try
{
startTransaction();
getSession().clear();
getSession().update(vo);
closeTransaction();
}
catch (HibernateException e)
{
rollbackTransaction();
e.printStackTrace();
}
}
当用户文件的某些值发生更改时,我想更新我的文件。 因此我使用:
daoFiles.update(file); // this uses the HibernateDAO update!
UserDAO daoUser = UserDAO.getInstance();
daoUser.update(user); //... also a HibernateDAO update
然后将文件的更改保存到数据库中,但是如果我想显示用户的文件,那么我无法更改文件,因为显示了旧值。只有当我直接获取 fileVO 时,我才能看到新值。所以看来 userVO 无法识别文件的更新。
有谁知道问题出在哪里?
最好的祝愿!
本尼
in my application I've got users which have files. The UserVO has the following mapping:
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "users")
public Set<Files> getFiles()
{
return this.files;
}
Then I have a HibernateDAO with the following update method:
public void update(T vo)
{
try
{
startTransaction();
getSession().clear();
getSession().update(vo);
closeTransaction();
}
catch (HibernateException e)
{
rollbackTransaction();
e.printStackTrace();
}
}
When some values of a user's file have changed, I want to update my file.
Therefore I use:
daoFiles.update(file); // this uses the HibernateDAO update!
UserDAO daoUser = UserDAO.getInstance();
daoUser.update(user); //... also a HibernateDAO update
The file's changes are saved to the database then but if I want to display the files of a user, then I cannot the changes of the files because the old values were displayed. Only if I get the fileVO directly I can see the new values. So it seems that the userVO does not recognize the update of a file.
Does anybody know were the problem is?
Best wishes!
Benny
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜问题是你的 userVO 和数据库不同步。您可以尝试从数据库重新加载 userVO 对象。但我更喜欢使用 hibernates 传递持久性功能。您可以通过将文件添加到用户对象并更新用户对象来保存文件。看一下传递持久性。
i guess the problem is that your userVO and the database get out of sync. You could try to reload the userVO object from the database. But I would prefer to use hibernates transitive persistence feature. You can save the files by adding it to the user object and updating the user object. Take a look at transitive persistence.