我的应用程序是用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)
我可能错过了一些关键的东西。我做错了什么?
//雅各布