1

I'm using vanilla Spring MVC with a custom FlashScope implementation. We basically use it to follow the Post-Redirect-Get pattern.

I've run into an odd situation. In my Post I do some searching for the parameters the user entered, and I set those instances onto FlashScope. I see those pieces working just fine.

In the object I place onto FlashScope I have a lazy loaded collection, however when I attempt to access the collection like so:

entity.getLazyLoadedCollection();

I receive the following stacktrace:

ERROR org.hibernate.LazyInitializationException IP127.0.0.1 CV#4c44559c-c576-4732 P#75004 - could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at core.model.entities.WorkflowState_$$_javassist_36.getFunctions(WorkflowState_$$_javassist_36.java)

The odd thing is that right before the call above I merge it onto the session in my service layer:

getSession().merge(entity);

Hibernate Documentation states that I should call update if I know that I'm working with a new session, but the JavaDocs make it seem like I should call merge...

As a workaround I've done the following:

if (getSession().contains(entity)) {
    getHibernateTemplate().merge(person);
} else {
    getSession.update(entity);
}

What should I be doing here?

2 Answers 2

5

Read the javadoc that you linked carefully: "Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the session."

No matter what you pass to merge(), that object doesn't get associated to the session. You need to be working with the object returned from merge().

Sign up to request clarification or add additional context in comments.

2 Comments

I gleened over the last bit about it not becoming associated with the session. Does the guard appear to be appropriate or should that be something else entirely?
I'm not sure there's a great answer to that. Other than the difference you found--associating an object to the session or not--update() and merge() accomplish roughly the same thing. I think it's probably just a matter of what semantically fits better into your system.
0

Never worked with FlashScope, but your error apeears to be because you are trying to access a LAZY collection which has not been initialized, and you are no longer in a layer of your app which has access to the Hibernate Session. If that assumption is correct, you need to initialize the collection where you have access to the Session (e.g., your DAO). Here are 2 basic ways:

Hibernate.initialize(object.getMyLazyCollection());

or

if(object.getMyLazyCollection() != null) {
   object.getMyLazyCollection().size(); // forces a collection load
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.