Gufran Khurshid
Gufran Khurshid

Reputation: 939

Spring Hibernate giving LazyInitializationException on Google App Engine

Consider entity

public class User {
...
@OneToMany(cascade = CascadeType.ALL)
List<SocialCredential> credentialsList = new ArrayList<SocialCredential>();
}

with DAO Implementation method

@Transactional
@Override
public User getUser(long id){
  Session s = sessionFactory.getCurrentSession();
  User asu = (User) s.get(User.class, id);
  return asu;
}

and Controller

@Controller
public class DummyController {
  @Autowired
  UserDAO userDAO;

  public void anyMethodAccessedByGetORPost(){
    User u= userDAO.getUser(1L);
  }
}

My question is why a simple query for entity User automatically fires query to initialize entity list of SocialCredential ? Ultimately it leads to LazyInitializationException.Is there anything wrong with Google App Engine.I have tried the same on Apache Tomcat which fires one query without eagerly initializing entity list SocialCredential with success.Afterwards ,I have used both local Jetty server and also tried after deploying it to GAE Server,but no success.I am not interested to EAGERLY load list SocialCredential.

Upvotes: 1

Views: 51

Answers (1)

sitakant
sitakant

Reputation: 1878

Use OpenSessionInViewFilter filter to open the session on filter level. Once after that you'll not get this kind of error.

Upvotes: 2

Related Questions