我的应用程序是用 Seam(2.2.1.CR2) 编写的,在最近的一次更新中,我为我的用户实体实现了一个名为 points 的字段。这个字段应该包含从许多其他字段和操作聚合的点,所以由于我有一个包含大量用户的数据库,我认为动态更新这些用户是个好主意。我通过将 User 实体中的新列分配给 -1 来做到这一点,然后每当调用 getPoints 时抛出一个事件并更新点,但只有一次。
@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)
我可能在这里遗漏了一些重要的东西。我究竟做错了什么?
// 雅各布