开发者

Hibernate One-To-Many Could Not Initialise Collection

开发者 https://www.devze.com 2023-04-03 16:23 出处:网络
I have two database tables, User and PageComment. Using Hibernate, I\'m trying to store a Set of PageComment objects in the User comment (comments made to that user), by using one-to-many relationship

I have two database tables, User and PageComment. Using Hibernate, I'm trying to store a Set of PageComment objects in the User comment (comments made to that user), by using one-to-many relationship in the hbm XML files.

The problem is, I seem to be able to retrieve the set from a User object, but as soon as I try to access any of the objects stored within the set, or even access on of the methods included in the set class (i.e. size()), the JVM throws "org.hibernate.exception.GenericJDBCException: could not initialize a collection". I'm at a loss on this one.

HBM For User Table:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="User" table="user">
    <id name="username" column="Username" type="string">
        <generator class="assigned"></generator>
    </id>
    <property name="password" column="Password" type="string"></property>
    <property name="firstname" column="Firstname" type="string"></property>
    <property name="surname" column="Surname" type="string"></property>
    <property name="email" column="Email" type="string"></property>
    <property name="admin" column="Admin" type="integer"></property>

    <set name="commentsMadeTo" inverse="true">
        <key column="userMadeTo"/>
        <one-to-many class="PageComment"/>
    </set>
</class>
</hibernate-mapping>

HBM For PageComment:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="PageComment" table="PageComment">
    <composite-id>
        <key-property name="userMadeBy" column="UserMadeBy" type="string" />
        <key-property name="userMadeTo" column="UserMadeTo" type="string" />
        <key-property name="time" column="Time" type="integer" />
        <generator class="assigned"></generator>
    </composite-id>
    <property name="commentText" column="CommentText" type="string"></property>

    <many-to-one name="userMadeTo" column="Username" not-null="true" class="User" />
</class>
</hibernate-mapping>

I'm trying to test the mapping with this method:

Session session = sessionFactory.openSession();
User theUser = (User)session.createQuery("FROM User WHERE Username='Samat'").uniqueResult();
System.out.println("Trying to print out all comments made to 'Samat'");
Set<PageComment> theComments开发者_运维技巧 = theUser.getCommentsMadeTo();
for(PageComment p: theComments){
    System.out.println(p.getAllData());
}


I spot that there are some problems in the relationship mapping

HBM For User Table:

 <set name="commentsMadeTo" inverse="true">
        <key column="XXXXXXXX"/>
        <one-to-many class="PageComment"/>
  </set>

HBM For PageComment:

<many-to-one name="userMadeTo" column="XXXXXXX" not-null="true" class="User" />

The value XXXXX represents the column name on the "many" side (i.e PageComment table in your case) that associates to its "One" side. It should have the same value in both mapping hbm.

Try to change the hbm for user table to:

<set name="commentsMadeTo" inverse="true">
    <key column="Username"/>
    <one-to-many class="PageComment"/>
</set>
0

精彩评论

暂无评论...
验证码 换一张
取 消