-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: "could not initialize a collection" on one-to-many
PostPosted: Mon Mar 17, 2008 7:13 pm 
Newbie

Joined: Fri Feb 22, 2008 11:52 am
Posts: 6
Hello,
My problem is as follows:
I have a problem with 2 one-to-many associations using a join table.
My mapping works fine unless I add any of the two last associations (I tried formulating it in other ways but then my list just ends up empty). (The error is on one in this post, but if I commented the mapping relevant to it, the same would happen with the other one)

This is my mapping file
Code:
[...]
<hibernate-mapping>
    <class name="sw.BaseActivity" table="activities">
        <id name="id" column="id">
            <generator class="increment"/>
        </id>
        <discriminator type="string" column="type" />
       
        <property name="name"/>
       
        <subclass name="sw.Activity" discriminator-value="ACTIVITY" lazy="false">
            <list name="questions"  table="questions" lazy="false">
                <key column="activity" not-null="true"/>
                <list-index column="order"/>
                <one-to-many class="sw.Question" />
            </list>
        </subclass>
       
        <subclass name="sw.ActivityContainer" discriminator-value="ACTIVITYCONTAINER" >
            <list name="constraintList" table="linktoconstraints" lazy="false" >
                <key column="activity" not-null="true" />
                <list-index column="order"/>
                <many-to-many column="constraint" class="sw.Constraint" foreign-key="id" unique="true"/>
            </list>
           <list name="activities" table="containedactivities"  lazy="false">
                <key column="parent" not-null="true"/>
                <list-index column="order" />
                <many-to-many column="child" class="sw.BaseActivity" foreign-key="id" unique="true"/>
            </list>
        </subclass>
    </class>
</hibernate-mapping>

This is a "tree like" structure. Activity and ActivityContainer inherits from BaseActivity, only ActivityContainer may contain other activities.

The exception happens during fetching and is the following
Code:
could not initialize a collection: [sw.ActivityContainer.constraintList#2]
[Ljava.lang.StackTraceElement;@1d5a0


The prior SQL queries run are
Code:
Hibernate:
    /*
from
    BaseActivity as ba fetch all properties
where
    ba.id=2  */ select
        baseactivi0_.id as id0_,
        baseactivi0_.name as name0_,
        baseactivi0_.type as type0_
    from
        activities baseactivi0_
    where
        baseactivi0_.id=2
Hibernate:
    /* load collection sw.ActivityContainer.constraintList */ select
        constraint0_.activity as activity1_,
        constraint0_.constraint as constraint1_,
        constraint0_.order as order1_,
        constraint1_.id as id3_0_,
        constraint1_.linkedactivity as linkedac3_3_0_,
        constraint1_.parameter1 as parameter4_3_0_,
        constraint1_.gap as gap3_0_,
        constraint1_.relatedactivity as relateda6_3_0_,
        constraint1_.const_type as const2_3_0_
    from
        linktoconstraints constraint0_
    left outer join
        constraints constraint1_
            on constraint0_.constraint=constraint1_.id
    where
        constraint0_.activity=?


My ActivityContainer class looks like this
Code:
public class ActivityContainer extends BaseActivity
{
    private List<Constraint> constraintList;
    private List<BaseActivity> activities;
[...]
public List<Constraint> getConstraintList() {
        return constraintList;
    }

    public void setConstraintList(List<Constraint> constraintList) {
        this.constraintList = constraintList;
    }
   public List<BaseActivity> getActivities() {
        return activities;
    }

    public void setActivities(List<BaseActivity> activityList) {
        this.activities = activityList;
    }
[...]
}


My database schema, for the relevant bits, is as following (postgresql)
Code:
CREATE TABLE activities (
    id integer NOT NULL,
    name text,
    type text,
    "order" integer
);

CREATE TABLE containedactivities (
    id integer NOT NULL,
    parent integer,
    child integer
);

CREATE TABLE linktoconstraints (
    id integer NOT NULL,
    activity integer,
    "constraint" integer
);


If I remove lazy="false", I no longer get the error, but my lists are empty then.

I am really clueless as why this is happening, I have to admit that this is my first hibernate project. What am I not doing correctly (it seemed to be similar to what I read into the documentation and samples, the main difference being the fact I disable lazy fetching).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 1:37 pm 
Newbie

Joined: Fri Feb 22, 2008 11:52 am
Posts: 6
I tried this on an other database engine and am experiencing the same problem, so it is unrelated to the choice of database engine.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 1:48 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Viiince wrote:
I tried this on an other database engine and am experiencing the same problem, so it is unrelated to the choice of database engine.



what is the full stack trace for the exception?



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 22, 2008 11:19 am 
Newbie

Joined: Fri Feb 22, 2008 11:52 am
Posts: 6
As for the full stack trace, here is something funny. I don't have one
My exception handling code is as follows (and displays full stack trace for other hibernate exceptions, but not for this one)

Code:
try {
        retreiveFromDB();
        } catch (Exception e)
        {
            System.out.println("An exception occured");
            System.out.println(e.getMessage());
            System.out.println(e.getCause().toString());
            System.out.println(e.getStackTrace());
        }
}


And this outputs the following:

Code:
An exception occured
could not initialize a collection: [rampupmonitor.GanttChart.ActivityContainer.activities#2]
org.postgresql.util.PSQLException: ERREUR: la colonne activities0_.order n'existe pas
[Ljava.lang.StackTraceElement;@131303f


The PSQLException is in french for some reason and means "ERROR: the column activities0_.order doesn't exist". (While it does exist in the database).

And for some other weird reason, the stack trace doesn't display.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 22, 2008 1:33 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
If I understand correctly it is looking for order field in the relations table since it is a many-to-many relation and there is no order field in the relations tables. As for the stack trace, you don't get anything because you are printing the object itself. You need to call something like e.printStackTrace().



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 22, 2008 2:38 pm 
Newbie

Joined: Fri Feb 22, 2008 11:52 am
Posts: 6
Indeed, I did think the order column had to belong to the other end of the relationship rather than to the relationship table.

This makes sense now.

Thank you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.