English 中文(简体)
EntityManager没有持久化合并,刷新时我得到了一个无法初始化代理的结果-没有抛出会话
原标题:EntityManager is not persisting with merge and when flushed i get a could not initialize proxy - no Session is thrown

我的应用程序是用Seam(2.2.1.CR2)编写的,在最近的一次更新中,我为我的用户实体实现了一个名为points的字段。这个字段应该包含从许多其他字段和操作中聚合的点,因此,由于我有一个包含大量用户的数据库,我认为动态更新这些用户是一个好主意。我通过将User实体中的新列赋值为-1来实现这一点,然后每当调用getPoints时,都会抛出一个Event并更新这些点,但只有一次。

@Entity(name = "User")
public class User {

@Column(nullable = false, columnDefinition = "int(11) default -1")
    private int points;

public int getPoints() {
        if (points == -1) {
            Events.instance().raiseEvent(AppreciatedInitialUpdate.EVENT_NAME, this);
        }
        return points;
    }
}

然后我创建了一个pointsBean:

@Name("pointsBean")    
@AutoCreate
public class PointsBean implements Serializable, AppreciatedInitialUpdate {

    @Logger
    private Log log;

    @In
    EntityManager entityManager

    @Observer(AppreciatedInitialUpdate.EVENT_NAME)
    public void handleAppreciatedInitialUpdate(User User) {
        this.calculateInitialPoints(user);
    }

    private void calculateInitialPoints(User user) {

        int likes = 0;

        List<Flag> appreciateFlags = user.getAppreciateFlags();
        likes = appreciateFlags.size();

        user.setLikes(likes);
        entityManager.merge(user);
        log.info("Initial update of points for user " + user.getId() + ". Now he/she has " + points);
    }           
}

但正如我的标题所述,当这样做时,实体不会被持久化,如果我刷新,那么我会得到一个异常:

[org.hibernate.LazyInitializationException] could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:62)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:116)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:166) 

我可能错过了一些关键的东西。我做错了什么?

//雅各布

最佳回答

可能user变量是在一个较早的会话中从数据库中检索到的,该会话已经关闭,因此当您调用user.getAppreciateFlags()时,这可能是一个LAZY数据库引用,它试图从数据库中检索它们,由于会话已关闭,因此发生了错误。

一个快速修复方法(不确定是否是最好的)是在getAppeciateFlags调用之前合并会话中的用户,因此代码将是:

entityManager.merge(user);
List<Flag> appreciateFlags = user.getAppreciateFlags();
likes = appreciateFlags.size();
user.setLikes(likes);
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签