Hello everybody:
I have the following classes:
LogActivity <-- Comment (LogActivity mother of comment)
Model
In addition a model contains a reference to a set of comments and each LogActivity is about (one-to-one) a model.
In practice the Hibernate mapping files are:
Code:
<class name="db.data.persistence.hibernate.Model" table="models" <id column="ID" name="id">>
<generator class="increment"/>
</id>
<property column="type" name="type" type="string"/>
<property column="name" name="name" type="string"/>
<property column="keywords" name="keywords" type="string"/>
<set inverse="true" lazy="true" name="comments" table="logactivities">
<key column="modelId"/>
<one-to-many class="db.data.persistence.hibernate.Comment"/>
</set>
<set inverse="true" lazy="true" name="activities" table="logactivities">
<key column="modelId"/>
<one-to-many class="db.data.persistence.hibernate.LogActivity"/>
</set>
</class>
and :
Code:
<class name="db.data.persistence.hibernate.LogActivity" table="logactivities">
<id column="ID" name="id">
<generator class="increment"/>
</id>
<property column="type" name="type" type="string"/>
<property column="time" name="time" type="date"/>
<property column="otherInfo" name="otherInfo" type="string"/>
<many-to-one class="db.data.persistence.hibernate.GeometricalModel" column="modelId" name="model" not-null="true"/>
<many-to-one class="db.data.persistence.hibernate.User" column="userId" name="user" not-null="true"/>
<joined-subclass name="db.data.persistence.hibernate.Comment" table="comments">
<key column="ID"/>
<property column="text" name="text" type="string"/>
<many-to-one column="respondsTo" name="respondsTo" class="db.data.persistence.hibernate.Comment"/>
<set inverse="true" lazy="true" name="isRespondedBy" table="commentsresponses">
<key column="cId"/>
<one-to-many class="db.data.persistence.hibernate.Comment"/>
</set>
</joined-subclass>
</class>
I would like to retrieve all the comments for a given model. This, in theory, can be easily be done with the following statements:
Code:
Session hibSession = HibernateUtil.getSessionFactory().getCurrentSession();
hibSession.beginTransaction();
Integer gmIdi = 3;
Criterion modelIdCriterion = Expression.eq("id", gmIdi);
Criteria mcriteria = hibSession.createCriteria(GeometricalModel.class);
mcriteria.add(modelIdCriterion);
GeometricalModel model = (GeometricalModel) mcriteria.uniqueResult();
Set<Comment> listOfComments = model.getComments();
hibSession.getTransaction().commit();
However, this gives the following exception:
ERROR - Unknown column 'comments0_.modelId' in 'field list'
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not initialize a collection: [db.data.persistence.hibernate.GeometricalModel.comments#3]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2001)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:109)
at org.hibernate.collection.PersistentSet.size(PersistentSet.java:139)
I understand that the problem is in the mapping on the relation between the model and the comments, however I don't understand what it is wrong with that - indeed, if all the log activities are comments, then no exception is raised.
Would you suggest looking at something in particular?
Thanks!
Dan[/code]