0
我面临与更改处于分离状态的持久对象相关联的问题。这些更改确实会影响服务器中的某个对象,但由于某些原因不会影响对象在数据库中的映射。休眠:更改对象处于分离状态后的持久性
我有什么:
两个实体与一个-2-许多assosiation链接:简介 - >文件。我已经配置了文件列表,我尝试添加新文件。
什么问题:
我首先清洁配置文件的文件列表(在II段),然后我添加新的文件来分析(在三节)。毕竟,数据库中的我拥有所有文件,包括被profile.getFileList()移除的文件。clear();
问:
为什么毕竟我在DB的旧文件,如果我继续profile.getFileList()明确的()。?
我的代码:
//I. pull the profile from BD
Session session = sessionFactory.openSession();
Profile existingProfile = (Profile) session.get(Profile.class, profileId);
session.close();
//II. make the copy of profile, out of the session scope, and clear file list of it
String serializedProfile = convertToJson(profile);
profile = Utils.jsonToObject(serializedProfile , Profile.class);
profile.getFileList(); // not empty!
profile.getFileList().clear();
//III. add new files to profile's file list
Session session1 = sessionFactory.openSession();
session1.beginTransaction();
profiles.getFileList().addAll(additionalFileList);
session1.saveOrUpdate(profile);
session1.getTransaction().commit();
session1.close();
没有它不是之前所说的convertToJson(配置文件)。我调用convertToJson(配置文件)以进行清除()操作超出会话范围。实际上,它是我的客户端 - 服务器迭代的简化模型。 –