My application is written in Seam(2.2.1.CR2) and in a recent update I implemented a field called points for my User entity. This field should hold points aggregated from a numerous other fields and actions, so since I have a database with tons of users I thought that updating these users on the fly would be a good idea. I did this by assigning the new column in the User entity to -1 and then whenever getPoints is invoked throw an Event and update the points, but only one time.
@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;
}
}
I then created a 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);
}
}
But as my title states when doing like this the entity is not persisted and if I flush then i get an Exception:
[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)
I'm probably missing something crucial here. What am I doing wrong?
// Jakob