LazyInitializationException添加到使用hibernate和gilead for gwt的实体类中的列表中时 [英] LazyInitializationException when adding to a list that is held within a entity class using hibernate and gilead for gwt

查看:87
本文介绍了LazyInitializationException添加到使用hibernate和gilead for gwt的实体类中的列表中时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用hibernate gilead和gwt来将我的数据保存在网站的用户和文件中。我的用户有一个文件位置列表。我正在使用注释将我的类映射到数据库。当我尝试将文件位置添加到用户类中保存的列表中时,我得到了一个 org.hibernate.LazyInitializationException



下面的方法是从我正在使用的外部文件上传servlet类中重写的。当文件上传时它会调用这个方法。

user1从别处的数据库加载。
user1.getFileLocations()。add(fileLocation); >处发生异常。我真的不明白这一点。!任何帮助都会很棒。错误的堆栈跟踪在

  public String executeAction(HttpServletRequest request,
List< FileItem> sessionFiles)throws UploadActionException {
for(FileItem item:sessionFiles){
if(false == item.isFormField()){
try {
YFUser user1 =(YFUser)getSession()。的getAttribute(SESSION_USER);

//这是存储文件的位置

String fileLocationString =/ Users / Stefano / Desktop / UploadedFiles /+ user1.getUsername();
文件fl = new File(fileLocationString);
fl.mkdir();
//所以这里我将为我上传的文件创建一个文件容器

文件文件= File.createTempFile(upload-,.bin,fl);


//这是文件写入磁盘的位置

item.write(file);

//然后创建FileLocation对象
FileLocation fileLocation = new FileLocation();
fileLocation.setLocation(fileLocationString);
// test
System.out.println(file path =+ file.getPath());



user1.getFileLocations()。add(fileLocation);

//上面的行是发生异常的地方
$ b $ catch(Exception e){
throw new UploadActionException(e.getMessage());
}
}
removeSessionFileItems(request);
}
返回null;
}

//这是Your Files User的类文件



pre $ @Entity
@Table(name =yf_user_table)
public class YFUser实现Serializable,ILightEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =user_id,nullable = false)
private int userId;
@Column(name =username)
私人字符串用户名;
@Column(name =password)
private String password;
@Column(name =email)
private String email;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name =USER_FILELOCATION,joinColumns = {
@JoinColumn(name =user_id)},inverseJoinColumns = {
@JoinColumn(name =locationId)})
private List< FileLocation> fileLocations = new ArrayList< FileLocation>();

public YFUser(){

}

public int getUserId(){
return userId;
}

private void setUserId(int userId){
this.userId = userId;
}

public String getUsername(){
return username;
}

public void setUsername(String username){
this.username = username;
}

public String getPassword(){
return password;
}

public void setPassword(String password){
this.password = password;
}

public String getEmail(){
return email;
}

public void setEmail(String email){
this.email = email;
}

public List< FileLocation> getFileLocations(){
if(fileLocations == null){
fileLocations = new ArrayList< FileLocation>();
}
返回fileLocations;

}

public void setFileLocations(List< FileLocation> fileLocations){
this.fileLocations = fileLocations;
}
/ *
public void addFileLocation(FileLocation location){
fileLocations.add(location);
} * /

@Override
public void addProxyInformation(String property,Object proxyInfo){
// TODO自动生成的方法存根


$ b @Override
public String getDebugString(){
// TODO自动生成的方法存根
返回null;
}

@Override
public Object getProxyInformation(String property){
// TODO自动生成的方法存根
返回null;
}

@Override
public boolean isInitialized(String property){
// TODO自动生成的方法存根
return false;
}

@Override
public void removeProxyInformation(String property){
// TODO自动生成的方法存根

}

@Override
public void setInitialized(String property,boolean initialised){
// TODO自动生成的方法存根

}

@Override
public Object getValue(){
// TODO自动生成的方法存根
返回null;
}

}



<$ p
$ b @Table(name =fileLocationTable)
public class FileLocation实现Serializable {

@Id
@GeneratedValue (strategy = GenerationType.AUTO)
@Column(name =locationId,updatable = false,nullable = false)
private int ieId;
@Column(name =location)
私人字符串位置;

$ b public FileLocation(){

}

public int getIeId(){
return ieId;
}

private void setIeId(int ieId){
this.ieId = ieId;
}

public String getLocation(){
return location;
}

public void setLocation(String location){
this.location = location;
}

}

  2010年4月2日上午11:33:12 org.hibernate.LazyInitializationException< init> 
SEVERE:未能懒惰地初始化一个角色集合:com.example.client.YFUser.fileLocations,没有会话或会话已关闭
org.hibernate.LazyInitializationException:无法延迟初始化角色集合: com.example.client.YFUser.fileLocations,没有会议或会议于在org.hibernate.collection.AbstractPersistentCollection org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
封闭
。 throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
at org.hibernate.collection.AbstractPersistentCollection.write(AbstractPersistentCollection.java:205)
at org.hibernate.collection.PersistentBag.add(PersistentBag.java:297)
at com.example.server.TestServiceImpl.saveFileLocation(TestServiceImpl.java:132)
at sun.reflect .NativeMet hodAccessorImpl.invoke0(本机方法)
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
。在java.lang.reflect.Method.invoke(Method.java:597)
at net.sf.gilead.gwt.PersistentRemoteService.processCall(PersistentRemoteService.java:174)
at com.google.gwt。 user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:224)
,位于com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)$ java
。 servlet.http.HttpServlet.service(HttpServlet.java:713)
位于javax.servlet.http.HttpServlet.service(HttpServlet.java:806)
位于org.mortbay.jetty.servlet.ServletHolder。处理(ServletHolder.java:487)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler。 java:216)
在org.mortbay.jetty.serv let.SessionHandler.handle(SessionHandler.java:181)
在org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
在org.mortbay.jetty.webapp.WebAppContext。 (org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler。 java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection $ RequestHandler.content(HttpConnection.java:843)
at org.mortbay .jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection .java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:396)
at org.mortb ay.thread.QueuedThreadPool $ PoolThread.run(QueuedThreadPool.java:488)
Apr 2,2010 11:33:12 PM net.sf.gilead.core.PersistentBeanManager clonePojo
INFO:第三方实例,未克隆:org.hibernate.LazyInitializationException:无法懒惰地初始化角色集合:com.example.client.YFUser.fileLocations,没有会话或会话已关闭


解决方案

Lazy意味着只有在访问数据库时才从数据库加载集合的值。如果当时关闭 Session ,则会抛出 LazyInitializationException ,因为无法提取数据。



在你的情况下,我只是建议在关联中添加一个渴望获取类型:

  @ManyToMany(cascade = CascadeType.ALL,fetchType = FetchType.EAGER)

当实体被加载并且不需要延迟加载时加载 fileLocations 。一个常见的解决方案是使用 OpenSessionInView ,但它可能不总是与GWT一起工作,因为客户端是远程的,并且会话不能在那里打开。



所以你会遇到一些懒惰初始化的问题。你可以搜索相关的问题,有几个 - 例如。

Right so i am working with hibernate gilead and gwt to persist my data on users and files of a website. my users have a list of file locations. i am using annotations to map my classes to the database. I am getting a org.hibernate.LazyInitializationException when i try to add file locations to the list that is held in the user class.

this is a method below that is overridden from a external file upload servlet class that i am using. when the file uploads it calls this method.

the user1 is loaded from the database elsewhere. the exception occurs at user1.getFileLocations().add(fileLocation); . I dont understand it really at all.! any help would be great. the stack trace of the error is below

public String executeAction(HttpServletRequest request,
            List<FileItem> sessionFiles) throws UploadActionException {
          for (FileItem item : sessionFiles) {
              if (false == item.isFormField()) {
                try {
                    YFUser user1 = (YFUser)getSession().getAttribute(SESSION_USER);

                    // This is the location where a file will be stored

                    String fileLocationString = "/Users/Stefano/Desktop/UploadedFiles/" + user1.getUsername();
                    File fl = new File(fileLocationString);
                    fl.mkdir();
                    // so here i will create the a file container for my uploaded file

                  File file = File.createTempFile("upload-", ".bin",fl);


                  // this is where the file is written to disk

                  item.write(file);

                  // the FileLocation object is then created
                  FileLocation fileLocation = new FileLocation();
                  fileLocation.setLocation(fileLocationString);
                  //test
                  System.out.println("file path = "+file.getPath());



                  user1.getFileLocations().add(fileLocation);

                  //the line above is where the exception occurs 

                } catch (Exception e) {
                  throw new UploadActionException(e.getMessage());
                }
              }
              removeSessionFileItems(request);
            }
            return null;
    }  

//This is the class file for a Your Files User

@Entity
@Table(name = "yf_user_table")
public class YFUser implements Serializable,ILightEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id",nullable = false)
private int userId;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
@Column(name = "email")
private String email;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "USER_FILELOCATION", joinColumns = { 
        @JoinColumn(name = "user_id") }, inverseJoinColumns = { 
        @JoinColumn(name = "locationId") })
private List<FileLocation> fileLocations = new ArrayList<FileLocation>() ;

public YFUser(){

}

public int getUserId() {
    return userId;
}

private void setUserId(int userId) {
    this.userId = userId;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public List<FileLocation> getFileLocations() {
    if(fileLocations ==null){
        fileLocations = new ArrayList<FileLocation>();
    }
    return fileLocations;

}

public void setFileLocations(List<FileLocation> fileLocations) {
    this.fileLocations = fileLocations;
}
/*
public void addFileLocation(FileLocation location){
    fileLocations.add(location);
}*/

@Override
public void addProxyInformation(String property, Object proxyInfo) {
    // TODO Auto-generated method stub

}

@Override
public String getDebugString() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public Object getProxyInformation(String property) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public boolean isInitialized(String property) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void removeProxyInformation(String property) {
    // TODO Auto-generated method stub

}

@Override
public void setInitialized(String property, boolean initialised) {
    // TODO Auto-generated method stub

}

@Override
public Object getValue() {
    // TODO Auto-generated method stub
    return null;
}

}

@Entity
@Table(name = "fileLocationTable")
public class FileLocation implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "locationId", updatable = false, nullable = false)
private int ieId;
@Column (name = "location")
private String location;


public FileLocation(){

}

public int getIeId() {
    return ieId;
}

private void setIeId(int ieId) {
    this.ieId = ieId;
}

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

}

Apr 2, 2010 11:33:12 PM org.hibernate.LazyInitializationException <init>
SEVERE: failed to lazily initialize a collection of role: com.example.client.YFUser.fileLocations, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.example.client.YFUser.fileLocations, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
at org.hibernate.collection.AbstractPersistentCollection.write(AbstractPersistentCollection.java:205)
at org.hibernate.collection.PersistentBag.add(PersistentBag.java:297)
at com.example.server.TestServiceImpl.saveFileLocation(TestServiceImpl.java:132)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at net.sf.gilead.gwt.PersistentRemoteService.processCall(PersistentRemoteService.java:174)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:224)
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:713)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:396)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
Apr 2, 2010 11:33:12 PM net.sf.gilead.core.PersistentBeanManager clonePojo
INFO: Third party instance, not cloned : org.hibernate.LazyInitializationException:     failed to lazily initialize a collection of role: com.example.client.YFUser.fileLocations, no session or session was closed

解决方案

Lazy means that the values of the collection are loaded from the database only when they are accessed. If at that time the Session has been closed, the LazyInitializationException is thrown, because the data cannot be fetched.

In your case I'd simply suggest adding an eager fetch type to the association:

@ManyToMany(cascade = CascadeType.ALL, fetchType=FetchType.EAGER)

This will load the fileLocations when the entity is loaded and no lazy loading will be needed.

A common solution is to use OpenSessionInView, but it may not always work with GWT, becaucse the client is remote and the session cannot be opened there.

So you are going to have some problems with lazy initialization. You can search around for related questions, there are a few - this and this for example.

这篇关于LazyInitializationException添加到使用hibernate和gilead for gwt的实体类中的列表中时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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