Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6535595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:23:30+00:00 2026-05-25T10:23:30+00:00

I have two database tables, User and PageComment. Using Hibernate, I’m trying to store

  • 0

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());
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:23:31+00:00Added an answer on May 25, 2026 at 10:23 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple database with two tables. Users and Configurations. A user has
Let's say I have two tables: Report Comment And assuming I have a database
I have two tables in my database, called ratings and movies . Ratings: |
I have two tables in my database: NEWS table with columns: id - the
I have two simple tables in my database. A card table that contains Id,
Here is a simplified version of my database model. I have two tables: Image,
I have a database with two tables ( Table1 and Table2 ). They both
I have a database with two main tables notes and labels . They have
I have two database tables, users and logs. I need SQL code to do
I have two tables User id name Result id user_id score created_at Every hour

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.