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).