3

I am facing Transaction errors such:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: app.Parent.children, could not initialize proxy - no Session

Here on parent.getChildren().size().

But I do have an @Transactional on @Service methods:

  • 1 transaction on createFactory() to create a factory and give the entitymanager
  • 1 transaction on create() to create the entity
@Path("api/parents")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Controller
public class ParentResource {

    @Autowired
    private ParentService parentService;

    @POST
    public Parent create(Parent parent) {
        ParentFactory parentFactory = parentService.createFactory(parent);
        return parentService.create(parent);
    }

}

@Service
public class ParentService {

    @PersistenceContext
    private EntityManager em;

    @Transactional
    public ParentFactory createFactory(Parent parent) {
        return new ParentFactory(parent, em);
    }

    @Transactional
    public Parent create(ParentFactory parentFactory) {
        return parentFactory.create();
    }

}

// No Transactional annotation
public class ParentFactory {

    private Parent parent;
    private EntityManager em;

    public ParentFactory(Parent parent, EntityManager em) {
        this.parent = parent;
        this.em = em;
    }

    public parent create() {
        if(parent.getChildren().size() < 3) { // Exception here
            em.persist(this.parent);
        }
    }

}

I'd like to be able to test when my Hibernate Session is alive or not, so that I can check when it's lost and why.

Thanks !

Pleymor
  • 2,611
  • 1
  • 32
  • 44
  • possible diuplicate : http://stackoverflow.com/questions/345705/hibernate-lazyinitializationexception-could-not-initialize-proxy – Som Bhattacharyya Mar 25 '16 at 10:41
  • Also checking session is alive or not is not going to help. You need to figure out why the exception is coming up. – Som Bhattacharyya Mar 25 '16 at 10:42
  • The session is closed as soon as the transactional method is finished. You have 2 transactional methods, each opening a new session/entitymanager. The session that was originally used for the `Parent` is already closed hence the error. Push that logic into a single transactional service methods (as it should imho) instead of in your controller. – M. Deinum Mar 25 '16 at 14:57
  • 1
    Thanks, however my question is more about how to check the session's status than why does this example fail actually. I fixed this problem already but I'd like to get a simple way to log if my hibernate session is open or closed :) – Pleymor Mar 25 '16 at 15:33

2 Answers2

0

I think the best way to handle your requirement is to use jpa validator @Size(min=3) on the children property

Olivier Boissé
  • 15,834
  • 6
  • 38
  • 56
  • 1
    Thank you @oliv37, but here my question is more about how to know when I have a closed session. – Pleymor Mar 25 '16 at 15:34
-1

Try with session.getTransaction().isActive()

shilpa
  • 17
  • 2