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 8340329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:09:08+00:00 2026-06-09T05:09:08+00:00

I am using Hibernate Many-to-Many association using the hibernate_reference.pdf found at hibernate site. I

  • 0

I am using Hibernate Many-to-Many association using the hibernate_reference.pdf found at hibernate site. I am using same entities Event.java and Person.java as described in the document.

I have seen many facing “could not initialize a collection exception” and many addressed the issue but none of the solution mentioned in the stack over flow posts fixed my issue. May be those solution do not work for Many-to-Many association or I am having some issue with my configuration. Following are my hbm.xml files:

Event.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="roseindia.tutorial.hibernate">
    <class name="Event" table="EVENTS">

        <id name="id" column="EVENT_ID">
            <generator class="native" />
        </id>

        <property name="date" type="timestamp" column="EVENT_DATE" />
        <property name="title" type="string" />
        <property name="location" type="string" column="LOC" />

        <set name="participants" table="PERSON_EVENT" inverse="true" lazy="false" cascade="all" >
            <key column="EVENT_ID" not-null="true" />
            <many-to-many column="PERSON_ID" class="Person" />
        </set>
    </class>
</hibernate-mapping>

Event.java

public class Event implements java.io.Serializable {
    private long id;
    private String title;
    private Date date;
    private String location;

    public Event() {
        //participants = new HashSet<Person>(0);
    }

    private Set participants = new HashSet(0);

    public Set getParticipants() {
        return participants;
    }

    public void setParticipants(Set participants) {
        this.participants = participants;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((date == null) ? 0 : date.hashCode());
        //result = prime * result + (int) (id ^ (id >>> 32));
        result = prime * result
                + ((location == null) ? 0 : location.hashCode());
        //result = prime * result + ((participants == null) ? 0 : participants.hashCode());
        result = prime * result + ((title == null) ? 0 : title.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Event other = (Event) obj;
        if (date == null) {
            if (other.date != null)
                return false;
        } else if (!date.equals(other.date))
            return false;

        if (location == null) {
            if (other.location != null)
                return false;
        } else if (!location.equals(other.location))
            return false;

        if (title == null) {
            if (other.title != null)
                return false;
        } else if (!title.equals(other.title))
            return false;
        return true;
    }

    @SuppressWarnings("unchecked")
    public void addToParticipants(Person person) {
        this.getParticipants().add(person);
        //person.addToEvent(this);
        person.getEvents().add(this);
    }

    public void removeFromParticipants(Person person) {
        this.getParticipants().remove(this);
        person.getEvents().remove(person);
    }

    public long getId() {
        return id;
    }

    private void setId(long id) {
        this.id = id;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getLocation() {
        return this.location;
    }

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

Person.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="roseindia.tutorial.hibernate">
    <class name="Person" table="PERSON">
        <id name="id" column="PERSON_ID">
            <generator class="native" />
        </id>
        <property name="age" />
        <property name="firstname" />
        <property name="lastname" />

        <set name="events" table="PERSON_EVENT" inverse="false" lazy="true" cascade="all" >
            <key column="PERSON_ID" not-null="true" />
            <many-to-many column="EVENT_ID" class="Event" />
        </set>

        <set name="emailAddresses" table="PERSON_EMAIL_ADDR">
            <key column="PERSON_ID" />
            <element type="string" column="EMAIL_ADDR" />
        </set>
    </class>
</hibernate-mapping>

Person.java

import java.util.HashSet;
import java.util.Set;

public class Person implements java.io.Serializable {
    private Set events = new HashSet<Event>();
    private Set emailAddresses = new HashSet();

    public Set getEmailAddresses() {
        return emailAddresses;
    }

    public void setEmailAddresses(Set emailAddresses) {
        this.emailAddresses = emailAddresses;
    }

    public void addToEvent(Event event) {
        this.getEvents().add(event);
        //event.addToParticipants(this);
        event.getParticipants().add(this);
    }

    public void removeFromEvent(Event event) {
        this.getEvents().remove(event);
        //event.removeFromParticipants(this);
        event.getParticipants().remove(this);
    }

    public Set getEvents() {
        return events;
    }

    public void setEvents(Set events) {
        this.events = events;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result
                + ((firstname == null) ? 0 : firstname.hashCode());
        result = prime * result
                + ((lastname == null) ? 0 : lastname.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Person other = (Person) obj;
        if (age != other.age)
            return false;

        if (firstname == null) {
            if (other.firstname != null)
                return false;
        } else if (!firstname.equals(other.firstname))
            return false;

        if (lastname == null) {
            if (other.lastname != null)
                return false;
        } else if (!lastname.equals(other.lastname))
            return false;
        return true;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    private Long id;
    private int age;
    private String firstname;
    private String lastname;

    public Person() {
    }
}

ManyToManyExample.java

public class ManyToManyExample {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Session session = null;
        try {
            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
            session = sessionFactory.openSession();
            Transaction tx1 = session.beginTransaction();

            System.out.println("Loading Record");
            Event theEvent = (Event) session.get(Event.class, new Long(1));
            session.refresh(theEvent);
            Person person = new Person();
            person.setAge(31);
            person.getEvents().add(theEvent);
            person.setFirstname("RRRRRR");
            person.setLastname("NNNNNN");
            person.getEmailAddresses().add("RRRRRR@yahoo.com");

            theEvent.getParticipants().add(person); // Adding person ref. to event for many to many relation

            session.save(theEvent);
            session.save(person);
            tx1.commit(); 

            System.out.println("Done...");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }
}

I get following error when I run above sample:
INFO DefaultLoadEventListener:129 – Error performing load command
org.hibernate.exception.GenericJDBCException: could not initialize a collection: [roseindia.tutorial.hibernate.Event.participants#1]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:82)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:70)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1351)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:101)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:484)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1346)
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:269)
at org.hibernate.engine.PersistenceContext.initializeNonLazyCollections(PersistenceContext.java:745)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:208)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1255)
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:139)
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:124)
at org.hibernate.persister.entity.BasicEntityPersister.load(BasicEntityPersister.java:2453)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:387)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:368)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:166)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:140)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:249)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:123)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:561)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:556)
at roseindia.tutorial.hibernate.ManyToManyExample.main(ManyToManyExample.java:24)
Caused by: java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataDouble(JdbcOdbc.java:3656)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataDouble(JdbcOdbcResultSet.java:5574)
at sun.jdbc.odbc.JdbcOdbcResultSet.getLong(JdbcOdbcResultSet.java:632)
at sun.jdbc.odbc.JdbcOdbcResultSet.getLong(JdbcOdbcResultSet.java:650)
at org.hibernate.type.LongType.get(LongType.java:26)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:77)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:68)
at org.hibernate.persister.collection.AbstractCollectionPersister.readKey(AbstractCollectionPersister.java:612)
at org.hibernate.loader.Loader.readCollectionElement(Loader.java:545)
at org.hibernate.loader.Loader.readCollectionElements(Loader.java:344)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:299)
at org.hibernate.loader.Loader.doQuery(Loader.java:384)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:203)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1344)
… 20 more

I create person object and add event to by fetching event using session.get(…). For many-to-many relation, after adding every object to person’s event list, i am doing otherway around and adding person to participants list in event object and that throws exception. Any helpful or guidance is deeply appreciated!

I am using SQL Server 2008 as database.

  • 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-06-09T05:09:09+00:00Added an answer on June 9, 2026 at 5:09 am

    try to change jdbc driver package , you can use jtds-1.2.jar ,and set driver class to net.sourceforge.jtds.jdbc.Driver

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Hibernate with JPA annotations. I have a one-directional many-to-many association that
We are using hibernate, postgres 8.3x Our entities are many to one mapped with
I am using HIbernate 3.2.5. I have a one-to-many association between Dept and Training
i have one problem in one-to-many mapping using hibernate. i have 2 classes, Person
I am using Hibernate in my project, and many of my entities use a
I am using hibernate many to many association. i have 3 tables(STUDENT,COURSE and STUDENT_COURSE).
I am trying to persist a one-to-many and a many-to-one relation using Hibernate 4.1.1
I am using Spring with Hibernate in my project.There are many methods written in
I'm trying to make simple many-to-one association, using NHibernate.. I have class Recruit with
Using hibernate with annotations, i want a one-many relationship to be sorted by the

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.