今天做了个hibernate的双向一对多,但老在paper.getReplys().add(reply)这里出错,我加个paper.getReply().size()也出错,错误如下:
java.lang.RuntimeException: net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection
com.bal.hibernate.replyService.addReply(replyService.java:49)
不知道什么原因,我把lazy设为false就出现另外的错误了,各位大虾,帮忙看下!!!!

解决方案 »

  1.   

    Session session = null;
    Transaction tx = null;
    try {
    session = SessionFactory.currentSession();
    tx = session.beginTransaction();
    Paper paper = (Paper)session.load(Paper.class,replyid);

    reply.setPaper(paper);
    //paper.getReplys().size();//这里出错
    paper.getReplys().add(reply);//去掉上面,这里也出错

    tx.commit();
    }catch(Exception e){
    try {
    tx.rollback();
    }catch(HibernateException ex) {
    System.out.println("reply add transaction error:" + ex);
    }
    System.out.println("reply add paper hibernate error:" + e);
    throw new RuntimeException(e);
    }finally {
    if (session != null) {
    try {
        SessionFactory.closeSession();
        }catch(HibernateException ex){
    System.out.println("addreply close session error:" + ex);
    throw new RuntimeException(ex);
        }
    }    
    }
      

  2.   

    这里 lazy 应该设成 false 了,看看你设成 false 时的错误 ~~
      

  3.   

    如果设为false就出现下面的错误Hibernate Error:net.sf.hibernate.exception.GenericJDBCException: could not initialize collection: [com.bal.hibernate.Paper.replys#1091]
      

  4.   

    把你的映射 配置文件还有pojo 一起贴出来帮你整一下
      

  5.   

    谢谢你了,下面是我的代码:
    Paper.hbm.xml:
    <?xml version="1.0" encoding='UTF-8'?>
    <!DOCTYPE hibernate-mapping PUBLIC
                                "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
                                "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" ><!-- DO NOT EDIT: This is a generated file that is synchronized -->
    <!-- by MyEclipse Hibernate tool integration.                   -->
    <!-- Created Thu Jan 12 15:56:36 CST 2006                         -->
    <hibernate-mapping package="com.bal.hibernate">    <class name="Paper" table="paper">
     
            <id name="id" column="id" type="short">
                <generator class="identity"/>
            </id>
            <property name="title" column="title" type="string" />
            <property name="content" column="content" type="string" />
            <property name="author" column="author" type="string" />
            <property name="createdate" column="createdate" type="timestamp" />
            <property name="hotcount" column="hotcount" type="integer" />
            <property name="replycount" column="replycount" type="integer" />
            <property name="computername" column="computername" type="string" />
            <property name="picture" column="picture" type="string" />
            <set name="replys" inverse="true" lazy="true" cascade="save-update">
              <key column="replyid"/>
              <one-to-many class="com.bal.hibernate.Reply"/>
            </set>
        </class>
        
    </hibernate-mapping>
    Reply.hbm.xml:
    <?xml version="1.0" encoding='UTF-8'?>
    <!DOCTYPE hibernate-mapping PUBLIC
                                "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
                                "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" ><!-- DO NOT EDIT: This is a generated file that is synchronized -->
    <!-- by MyEclipse Hibernate tool integration.                   -->
    <!-- Created Mon Feb 13 14:02:45 CST 2006                         -->
    <hibernate-mapping package="com.bal.hibernate">    <class name="Reply" table="reply">
     
            <id name="id" column="id" type="short">
                <generator class="identity"/>
            </id>
            
            <many-to-one name="paper" column="replyid" cascade="none" not-null="true"
                class="com.bal.hibernate.Paper">
                
            </many-to-one>
            
            <property name="title" column="title" type="string" />
            <property name="content" column="content" type="string" />
            <property name="author" column="author" type="string" />
            <property name="createdata" column="createdata" type="timestamp" />
            <property name="computername" column="computername" type="string" />
            <property name="picture" column="picture" type="string" />
            
            
        </class>
        
    </hibernate-mapping>
      

  6.   

    AbstractPaper.java:
    /*
     * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
     * by MyEclipse Hibernate tool integration.
     *
     * Created Thu Jan 12 15:56:36 CST 2006 by MyEclipse Hibernate Tool.
     */
    package com.bal.hibernate;import java.io.Serializable;
    import java.util.Set;
    import java.util.HashSet;/**
     * A class that represents a row in the paper table. 
     * You can customize the behavior of this class by editing the class, {@link Paper()}.
     * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
     * by MyEclipse Hibernate tool integration.
     */
    public abstract class AbstractPaper 
        implements Serializable
    {    /** The value of the simple id property. */
        private java.lang.Short id;    /** The value of the simple title property. */
        private java.lang.String title;    /** The value of the simple content property. */
        private java.lang.String content;    /** The value of the simple author property. */
        private java.lang.String author;    /** The value of the simple createdate property. */
        private java.util.Date createdate;    /** The value of the simple hotcount property. */
        private java.lang.Integer hotcount;    /** The value of the simple replycount property. */
        private java.lang.Integer replycount;    /** The value of the simple computername property. */
        private java.lang.String computername;    /** The value of the simple picture property. */
        private java.lang.String picture;    private Set replys = new HashSet();
        
        /**
         * Simple constructor of AbstractPaper instances.
         */
        public AbstractPaper()
        {
        }    /**
         * Return the value of the id column.
         * @return java.lang.Short
         */
        public java.lang.Short getId()
        {
            return this.id;
        }    /**
         * Set the value of the id column.
         * @param id
         */
        public void setId(java.lang.Short id)
        {
            this.id = id;
        }    /**
         * Return the value of the title column.
         * @return java.lang.String
         */
        public java.lang.String getTitle()
        {
            return this.title;
        }    /**
         * Set the value of the title column.
         * @param title
         */
        public void setTitle(java.lang.String title)
        {
            this.title = title;
        }    /**
         * Return the value of the content column.
         * @return java.lang.String
         */
        public java.lang.String getContent()
        {
            return this.content;
        }    /**
         * Set the value of the content column.
         * @param content
         */
        public void setContent(java.lang.String content)
        {
            this.content = content;
        }    /**
         * Return the value of the author column.
         * @return java.lang.String
         */
        public java.lang.String getAuthor()
        {
            return this.author;
        }    /**
         * Set the value of the author column.
         * @param author
         */
        public void setAuthor(java.lang.String author)
        {
            this.author = author;
        }    /**
         * Return the value of the createdate column.
         * @return java.util.Date
         */
        public java.util.Date getCreatedate()
        {
            return this.createdate;
        }    /**
         * Set the value of the createdate column.
         * @param createdate
         */
        public void setCreatedate(java.util.Date createdate)
        {
            this.createdate = createdate;
        }    /**
         * Return the value of the hotcount column.
         * @return java.lang.Integer
         */
        public java.lang.Integer getHotcount()
        {
            return this.hotcount;
        }    /**
         * Set the value of the hotcount column.
         * @param hotcount
         */
        public void setHotcount(java.lang.Integer hotcount)
        {
            this.hotcount = hotcount;
        }    /**
         * Return the value of the replycount column.
         * @return java.lang.Integer
         */
        public java.lang.Integer getReplycount()
        {
            return this.replycount;
        }    /**
         * Set the value of the replycount column.
         * @param replycount
         */
        public void setReplycount(java.lang.Integer replycount)
        {
            this.replycount = replycount;
        }    /**
         * Return the value of the computername column.
         * @return java.lang.String
         */
        public java.lang.String getComputername()
        {
            return this.computername;
        }    /**
         * Set the value of the computername column.
         * @param computername
         */
        public void setComputername(java.lang.String computername)
        {
            this.computername = computername;
        }    /**
         * Return the value of the picture column.
         * @return java.lang.String
         */
        public java.lang.String getPicture()
        {
            return this.picture;
        }    /**
         * Set the value of the picture column.
         * @param picture
         */
        public void setPicture(java.lang.String picture)
        {
            this.picture = picture;
        }
        
        public Set getReplys() {
         return this.replys;
        }
        
        public void setReplys(Set replys) {
         this.replys = replys;
        }
    }
      

  7.   

    AbstractReply.java:/*
     * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
     * by MyEclipse Hibernate tool integration.
     *
     * Created Mon Feb 13 14:02:46 CST 2006 by MyEclipse Hibernate Tool.
     */
    package com.bal.hibernate;import java.io.Serializable;/**
     * A class that represents a row in the reply table. 
     * You can customize the behavior of this class by editing the class, {@link Reply()}.
     * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
     * by MyEclipse Hibernate tool integration.
     */
    public abstract class AbstractReply 
        implements Serializable
    {    /** The value of the simple id property. */
        private java.lang.Short id;    /** The value of the simple replyid property. */
        private java.lang.Integer replyid;    /** The value of the simple title property. */
        private java.lang.String title;    /** The value of the simple content property. */
        private java.lang.String content;    /** The value of the simple author property. */
        private java.lang.String author;    /** The value of the simple createdata property. */
        private java.util.Date createdata;    /** The value of the simple computername property. */
        private java.lang.String computername;    /** The value of the simple picture property. */
        private java.lang.String picture;    private Paper paper;
        /**
         * Simple constructor of AbstractReply instances.
         */
        public AbstractReply()
        {
        }    public Paper getPaper() {
         return this.paper;
        }
        
        public void setPaper(Paper paper) {
         this.paper = paper;
        }
        
        /**
         * Return the value of the id column.
         * @return java.lang.Integer
         */
        public java.lang.Short getId()
        {
            return this.id;
        }    /**
         * Set the value of the id column.
         * @param id
         */
        public void setId(java.lang.Short id)
        {
            this.id = id;
        }    /**
         * Return the value of the replyid column.
         * @return java.lang.Integer
         */
        public java.lang.Integer getReplyid()
        {
            return this.replyid;
        }    /**
         * Set the value of the replyid column.
         * @param replyid
         */
        public void setReplyid(java.lang.Integer replyid)
        {
            this.replyid = replyid;
        }    /**
         * Return the value of the title column.
         * @return java.lang.String
         */
        public java.lang.String getTitle()
        {
            return this.title;
        }    /**
         * Set the value of the title column.
         * @param title
         */
        public void setTitle(java.lang.String title)
        {
            this.title = title;
        }    /**
         * Return the value of the content column.
         * @return java.lang.String
         */
        public java.lang.String getContent()
        {
            return this.content;
        }    /**
         * Set the value of the content column.
         * @param content
         */
        public void setContent(java.lang.String content)
        {
            this.content = content;
        }    /**
         * Return the value of the author column.
         * @return java.lang.String
         */
        public java.lang.String getAuthor()
        {
            return this.author;
        }    /**
         * Set the value of the author column.
         * @param author
         */
        public void setAuthor(java.lang.String author)
        {
            this.author = author;
        }    /**
         * Return the value of the createdata column.
         * @return java.util.Date
         */
        public java.util.Date getCreatedata()
        {
            return this.createdata;
        }    /**
         * Set the value of the createdata column.
         * @param createdata
         */
        public void setCreatedata(java.util.Date createdata)
        {
            this.createdata = createdata;
        }    /**
         * Return the value of the computername column.
         * @return java.lang.String
         */
        public java.lang.String getComputername()
        {
            return this.computername;
        }    /**
         * Set the value of the computername column.
         * @param computername
         */
        public void setComputername(java.lang.String computername)
        {
            this.computername = computername;
        }    /**
         * Return the value of the picture column.
         * @return java.lang.String
         */
        public java.lang.String getPicture()
        {
            return this.picture;
        }    /**
         * Set the value of the picture column.
         * @param picture
         */
        public void setPicture(java.lang.String picture)
        {
            this.picture = picture;
        }
    }