简体   繁体   English

SEAM /休眠:在对话期间,我无法从数据库中获取更改

[英]SEAM/Hibernate: I can't get changes from database during a Conversation

I have a class: 我有一堂课:

@Name("foo")
@Scope(ScopeType.CONVERSATION)
public class Foo {

    @In
    Session session;

    @In
    private Conversation conversation;

    @RequestParameter
    Long barId;

    /* outjected fields */
    @Out
    Bar bar;

    /* some attributes and methods */

    public void start() {
        /* some initializations */
        this.bar = (Bar) session.get(Bar.class, barId);
    }

    public void end() {
        /* some actions involving bar and bar.getBaz() */
        conversation.end();
    }        
}

The method start() is called at the beginning of the conversation and initializes the attribute bar (Bar is some entity class). 在对话开始时将调用方法start()并初始化属性bar(Bar是某些实体类)。 Then, bar has an attribute baz which is also an entity. 然后,bar具有属性baz,它也是一个实体。

During a conversation, it might happen that some other user modifies the baz object related to our bar. 在对话期间,其他用户可能会修改与我们的酒吧相关的baz对象。 Then, when end() is called, bar.getBaz() returns the old (unmodified) version of baz, and, moreover, overwrites it to the database. 然后,在调用end()时,bar.getBaz()返回baz的版本(未修改),并且将其覆盖到数据库中。

I tried to get the modified state of baz in the end() method using session.flush(), session.merge(bar), session.merge(bar.getBaz()), but nothing works. 我试图使用session.flush(),session.merge(bar),session.merge(bar.getBaz())在end()方法中获取baz的修改状态,但是没有任何效果。 How can I avoid pollution in the database? 如何避免数据库污染?

Sorry that I can't comeup with a elaborate answer. 抱歉,我无法给出详尽的答案。 I had to reload entities from the DB which where modified by another serverlet. 我不得不从数据库中重新加载由另一个serverlet修改过的实体。 I did this by using the refresh() method from JPA. 我通过使用JPA的refresh()方法做到了这一点。 A Hibernate session also provides the refresh() method. Hibernate会话还提供了refresh()方法。 You might also want to consider the @Version annotation for optimistic locking. 您可能还需要考虑使用@Version批注进行乐观锁定。

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

相关问题 如何从 Hibernate 和 Spring 获取数据库查询的打印输出 - How can I get a printout of a database query from Hibernate and Spring 如何从Hibernate获取自定义类型的数据库方言? - How can I get the database dialect from Hibernate for a Custom Type? 在与Tomcat Web服务器对话期间,是否应检查JSESSIONID是否更改? - during a conversation with a Tomcat web server shall I check if JSESSIONID changes? JBoss接缝:在ScopeType.PAGE中,我得到:java.lang.IllegalStateException:没有对话上下文处于活动状态 - JBoss Seam: In ScopeType.PAGE I get: java.lang.IllegalStateException: No conversation context active 对实体的更改不刷新到Seam 3 / Hibernate中的数据库 - Changes to Entity not flushing to DB in Seam 3 / Hibernate 为什么不能在控制器中的新线程中使用@In(使用jboss seam)注入依赖项? - Why can't I inject dependencies with @In (using jboss seam) in a new thread from within my controller? 休眠,无法从关系中获取数据 - Hibernate, can't get data from relations 无法使用 spring-mvc 和 hibernate 从数据库中获取数据 - can't get data from database using spring-mvc and hibernate 无法创建方法从数据库 hibernate mySQL + Spring mvc - Can't create method to get data from database hibernate mySQL + Spring mvc Hibernate不会在PostgreSQL数据库中进行更改 - Hibernate doesn't make changes in PostgreSQL database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM