简体   繁体   English

添加条目后,注释为“ Lazy”的地图未更新

[英]Map annotated as “Lazy” not updated after adding entries

I've the following Entity: 我有以下实体:

   class User {
    ..
    ...
    @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "id.uKey")
    @MapKey(name="id.achieveId")
    private Map<Integer, Achievement> achievements;
    ..
    ..
}

at some point I call: Hibernate.Initialize(); 在某个时候,我打电话给我:Hibernate.Initialize();

and this map is filled with entries from DB. 并且此映射充满了来自DB的条目。

when the app continues I save new entries into the DB table. 当应用继续时,我将新条目保存到数据库表中。

and then I try to access the map but it doesn't contain the new entries. 然后尝试访问该地图,但其中不包含新条目。

Is there a way to make it aware that it needs to re-select the DB table? 有没有一种方法可以使其意识到需要重新选择数据库表?

Thanks 谢谢

EDIT: 编辑:

I add new entries like this: 我添加这样的新条目:

public void save() {
    ..
    tx = dbs.beginTransaction();

    Achievement ua = new Achievement(key, id);
    dbs.save(ua);

    tx.commit();
}

After initializing your object, it resides in the hibernate session and this session is not designed to be updated by changes from the underlying database. 初始化对象后,该对象将驻留在休眠会话中,并且该会话不旨在通过基础数据库的更改进行更新。 Just reload the object from the database, but remove it from the session before doing so. 只需从数据库中重新加载对象,然后再将其从会话中删除即可。

But maybe, what you really want is to modify the map contained in your object. 但是也许,您真正想要的是修改对象中包含的地图。 That would be the OOP way. 那就是OOP的方式。 After that you could persist the entire object with Hibernate. 之后,您可以使用Hibernate保留整个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM