An old problem: could not initialize a collection: XXX

Category: Java EE
 
qianniao221
2013-08-20 03:26:34

Sponsored Links
org.apache.jasper.JasperException: could not initialize a collection: [com.coolgo.domain.model.Topic.posts # 1]
org.apache.jasper.servlet.JspServletWrapper . handleJspException (JspServletWrapper.java: 510)
org.apache.jasper.servlet.JspServletWrapper.service (JspServletWrapper.java: 393)
org.apache.jasper.servlet.JspServlet.serviceJspFile (JspServlet.java : 314)
org.apache.jasper.servlet.JspServlet.service (JspServlet.java: 264)
javax.servlet.http.HttpServlet.service (HttpServlet.java: 802)
com . coolgo.util.SetCharacterEncodingFilter.doFilter (SetCharacterEncodingFilter.java: 91)

a board table, a topic table, a post table
board table and topic table two-way-to-many relationship
topic table with post-many relationship table is bidirectional

Board.hbm.xml

<hibernate-mapping>
    <class name="com.coolgo.domain.model.Board" table="board" catalog="coolgo">
        <id name="boardId" type="java.lang.Integer">
            <column name="board_id" />
            <generator class="identity" />
        </id>
        <property name="boardIsMother" type="java.lang.String">
            <column name="board_isMother" length="6" />
        </property>
        <property name="boardBid" type="java.lang.Integer">
            <column name="board_bid" />
        </property>
        <property name="boardName" type="java.lang.String">
            <column name="board_name" length="300" />
        </property>  
        ........
        <set 
  name="topics" 
  cascade="all"     
  inverse="true" 
  lazy="false" >
   <key column="topic_boardid"/> 
   <one-to-many class="com.coolgo.domain.model.Topic"/> 
</set>
    </class>
</hibernate-mapping>


Topic.hbm.xml

<hibernate-mapping>
    <class name="com.coolgo.domain.model.Topic" table="topic" catalog="coolgo">
        <id name="topicId" type="java.lang.Integer">
            <column name="topic_id" />
            <generator class="identity" />
        </id>        
        <property name="topicUser" type="java.lang.String">
            <column name="topic_user" length="300" />
        </property>
        <property name="topicName" type="java.lang.String">
            <column name="topic_name" length="300" />
        </property>
  .....        
        <many-to-one name="board"
        column="topic_boardid"
        class="com.coolgo.domain.model.Board"
        lazy="false"
        not-null="true"></many-to-one>
         <span style="color: #FF0000;"><set 
   name="posts" 
   cascade="all"
   inverse="true" 
   lazy="false">
    <key column="post_topicid"/> 
    <one-to-many class="com.coolgo.domain.model.Post"/> 
 </set></span>    </class>
</hibernate-mapping>


Post.hbm.xml

<hibernate-mapping>
    <class name="com.coolgo.domain.model.Post" table="post" catalog="coolgo">
        <id name="postId" type="java.lang.Integer">
            <column name="post_id" />
            <generator class="identity" />
        </id>
        <property name="postBoardid" type="java.lang.Integer">
            <column name="post_boardid" />
        </property>
        <property name="postUser" type="java.lang.String">
            <column name="post_user" length="300" />
        </property>
        .........
        
        <many-to-one name="topic"
        column="post_topicid"
        class="com.coolgo.domain.model.Topic" 
        lazy="false"
        not-null="true"></many-to-one>
    </class>
</hibernate-mapping>


Board.java

public class Board implements java.io.Serializable {
private Integer boardId;
private Integer boardBid = 0;
private String boardName;
private String boardInfo;
private Set topics = new HashSet();
....

public Set getTopics() {
return topics;
}

public void setTopics(Set topics) {
this.topics = topics;
}
....
}


Topic.java
public class Topic implements java.io.Serializable {
private Integer topicId;
// private Integer topicBoardid;
private String topicUser;
private String topicName;
....
private Board board;
private Set posts = new HashSet();

public Board getBoard() {
return board;
}
public void setBoard(Board board) {
this.board = board;
}
public Set getPosts() {
return posts;
}
public void setPosts(Set posts) {
this.posts = posts;
}
....
}


Post.java
public class Post implements java.io.Serializable {
private Integer postId;
private Integer postBoardid;
private String postUser;
private Topic topic;
        ......
public Topic getTopic() {
return topic;
}

public void setTopic(Topic topic) {
this.topic = topic;
}
}


Note Topic.hbm.xml the red code is not added, all the normal tests, but after adding, java tests are normal, while the web test has emerged:

could not initialize a collection: [com.coolgo.domain.model.Topic.posts # 1] such errors.

and when the red code lazy = "false" to lazy = "true" when, web tests are normal, while the java test appears:
Exception in thread "main" org.hibernate.LazyInitializationException : failed to lazily initialize a collection of role: com.coolgo.domain.model.Topic.posts, no session or session was closed
.....
Why? (test both find test)

Sponsored Links

ihciat
2013-08-20 03:45:59
LazyInitializationException:

lazy loading error, with OpenSessioninView chant
sdjj111
2013-08-20 03:52:57
I think that seems to be your private Set posts = new HashSet (); This sentence has issues now, you put this set set to change it look
junyi00001
2013-08-20 04:00:19
to You hibernate code stickers.

wzd179185015
2013-08-20 04:19:38
Oh, try a floor.
2 floor I have to try, but that should not, ah, I was also in Board.java set up a private Set topics = new HashSet (); but will not go wrong,
according should private Set posts = new HashSet (); ; which will not bad either. give it a try.
xly_zh
2013-08-20 04:34:46
package com.coolgo.domain.dao;

import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.coolgo.domain.model.Post;

/**
 * A data access object (DAO) providing persistence and search support for Post
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.coolgo.domain.model.Post
 * @author MyEclipse Persistence Tools
 */

public class PostDAO extends BaseHibernateDAO {
private static final Log log = LogFactory.getLog(PostDAO.class);
// property constants
public static final String POST_BOARDID = "postBoardid";
public static final String POST_USER = "postUser";
public static final String POST_TOPICID = "postTopicid";
public static final String POST_REPLYID = "postReplyid";
public static final String POST_CONTENT = "postContent";
public static final String POST_IP = "postIp";

public void save(Post transientInstance) {
log.debug("saving Post instance");
try {
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}

public void delete(Post persistentInstance) {
log.debug("deleting Post instance");
try {
getSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}

public Post findById(java.lang.Integer id) {
log.debug("getting Post instance with id: " + id);
try {
Post instance = (Post) getSession().get(
"com.coolgo.domain.model.Post", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}

public List findByExample(Post instance) {
log.debug("finding Post instance by example");
try {
List results = getSession().createCriteria(
"com.coolgo.domain.model.Post").add(Example.create(instance))
.list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}

public List findByProperty(String propertyName, Object value) {
log.debug("finding Post instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Post as model where model."
+ propertyName + "= ?";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}

public List findByPostBoardid(Object postBoardid) {
return findByProperty(POST_BOARDID, postBoardid);
}

public List findByPostUser(Object postUser) {
return findByProperty(POST_USER, postUser);
}

public List findByPostTopicid(Object postTopicid) {
return findByProperty(POST_TOPICID, postTopicid);
}

public List findByPostReplyid(Object postReplyid) {
return findByProperty(POST_REPLYID, postReplyid);
}

public List findByPostContent(Object postContent) {
return findByProperty(POST_CONTENT, postContent);
}

public List findByPostIp(Object postIp) {
return findByProperty(POST_IP, postIp);
}

public List findAll() {
log.debug("finding all Post instances");
try {
String queryString = "from Post";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}

public Post merge(Post detachedInstance) {
log.debug("merging Post instance");
try {
Post result = (Post) getSession().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}

public void attachDirty(Post instance) {
log.debug("attaching dirty Post instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public void attachClean(Post instance) {
log.debug("attaching clean Post instance");
try {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
}

Is this it?
taly151
2013-08-20 04:53:01
Brother, you do not solve this problem ah? Cause I can not find how ah
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.