java - How to solve Hibernate's LazyInitializationException -


CustomerEntity has been mapped to the table in the table Customer . There are multiple connections to order and addresses in this now I retrieve the customer with the order with the next code:

  DetachedCriteria criteria = DetachedCriteria.forClass (CustomerEntity.class); Criterion Add (restriction. Eq ("id", patient id)); Criteria.createCriteria ("Order", CriteriaSpecification.LEFT_JOIN) .add (Restrictions.and (Restrictions.isNull ("deletedDate"), Restrictions.or (Restrictions.isNull ("old"), Restrictions.eq ("old", BoolType .false)))); Customer Interval Customer = Quarriage search criteria);   

Queries:

  public   

But when I customer.getAddresses Trying to apply () The next exception is thrown:

  org.hibernate.LazyInitializationException: Failed to initialize a collection of the role lazily: No, no session or session Closed   

This is because the default hibernate loads more than one unit How can I recover the customer unit its addresses without modifying it?

this exception occurs when you try to access a lazy-loading collection or field when session is not available. When application loads backend of the data and closes the session, you pass this data, for example, the frontend where there is no truth Already

This is some of the solution possible. Go ways:

  • is eager load reliable, but may slow down solution;
  • The way the session is managed, consider the strategy session-per-request which is suitable for a typical web application. Information in question can be useful to you

    For information about other session management strategies, try reading.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -