Spring Hibernate在Google App Engine上给出LazyInitializationException [英] Spring Hibernate giving LazyInitializationException on Google App Engine

查看:128
本文介绍了Spring Hibernate在Google App Engine上给出LazyInitializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑实体

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

$ / code>

使用DAO实现方法

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

和Controller

  @Controller 
public class DummyController {
@Autowired
UserDAO userDAO;

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






$ b我的问题是为什么一个简单的查询实体用户自动触发查询来初始化SocialCredential的实体列表?最终会导致LazyInitializationException。Google App Engine有什么问题。我已经尝试过在Apache Tomcat上启动同一个查询,而没有急切地初始化实体列表SocialCredential并且成功。之后,我使用了两个本地Jetty服务器,并且在部署它到GAE服务器,但没有成功。我对EAGERLY没有兴趣加载列表SocialCredential。

解决方案

使用OpenSessionInViewFilter过滤器打开过滤级别的会话。之后一次你不会得到这种错误。


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.

解决方案

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

这篇关于Spring Hibernate在Google App Engine上给出LazyInitializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆