0

I'm just a spring + hibernate beginner. I'm doing a simple project and I set up eveything for transactions, a simple service, a simple dao and all the annotated beans.

Using list works, I can list the objects and their properties, using a Criteria query. But if I try to load an object with:

Session sess = sessionFactory.getCurrentSession();
Ordine res = (Ordine) sess.load(Ordine.class, id);
return res;

the query seems to work fine, but I get a nasty exception when the View tries to read a property (a string):

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

I even noticed this is the startup logs:

INFO: Bean 'mySessionFactory' of type [class org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

What does all this mean?

4 Answers 4

4

You have a few choises the way I see it

  • Force your object to not be loaded lazily (e.g., set fetch=FetchType.EAGER on your persistence annotations).
  • OpenSessionInViewFilter
  • Invoke the methods of your routine in data layer (to load the values) before invoking them in your view.

[Edit] This does not at all seem to work as I initially suggested.

  • Add transactions. Annotate your controller routine using @Transactional.
Sign up to request clarification or add additional context in comments.

5 Comments

Annotate your controller routine using @Transactional Does that work? The view is beyond the controller's scope, isn't it?
I tried it out once, and if I remember correctly it behaved well (my memory might be hazy though). This of course assumes you have all spring-configuration needed for transaction scanning.
This answer is right, minus the @Transactional per Sean's comment.
I tried to put FetchType.Eager, no success. I put the OpenSessionInViewIntercepto.... nothing.
I tried both and can verify that they work. Can you illustrate with more code what you're doing? E.g., @OneToMany(..., fetch=...) ? Also, when using OSIV you should place it in the same appcontext as your controller definitions, see e.g. this.
0

You probably need to use the OpenSessionInViewFilter

Comments

0

Sean,

Johan's suggestion about setting fetch=FetType.EAGER is your simplest solution. You should be careful using this solution, though. Sometimes you can solve your problem by modifying your dao query to do a left join fetch on the related entity that you're interested in. eg:

Query q = this.em.createQuery("select x from PrimaryEntity x \n"
+ "left join fetch x.secondaryEntityField \n "
+ "where x.selectionProperty = :selection");

1 Comment

Uhm, Sean did not ask the question :-)
0

org.hibernate.Hibernate -class has some handy static utility methods, for example see Hibernate.initialize. You still need an open session for it though. The easiest ways are probably using OpenSessionInViewFilter, setting fetch-types to eager or modifying the query to load the lazy-relations, as suggested in the other answers.

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.