I want to manage a Transaction in my persistence layer,
But when I try to fetch the results lazily I get this error:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role
Can I use LockMode or any other way to solve this problem?
Can a find a Object by its id without any Transaction?
Your problem is that the Hibernate session is already closed when you try to access the content. Hibernate cannot load the content without a session. There are usually two ways to mitigate this problem:
Don’t close the session until you are done with the page. This pattern is called “session in view” and can for example be implemented with a servlet filter.
Initialize all contents that you’ll need before closing the session. If you want to initialize the whole object you can use Hibernate.initialize(object).
edit: You cannot do anything outside of an transaction in Hibernate.