EntityManager is not persisting with merge and when flushed i get a could not initialize proxy - no Session is thrown

StackOverflow https://stackoverflow.com/questions/10755826

  •  11-06-2021
  •  | 
  •  

Question

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

Was it helpful?

Solution

Probably the user variable was retrieved from the database in an older session, that was already closed, so when you call user.getAppreciateFlags();, that is probably a LAZY database reference, it tries to retrieve them from the database, and since the session is closed, that error happens.

One quick fix (not sure if its the best one), is to merge the user in the session before the getAppeciateFlags call, so the code will be:

entityManager.merge(user);
List<Flag> appreciateFlags = user.getAppreciateFlags();
likes = appreciateFlags.size();
user.setLikes(likes);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top