Hi hibernate masters !
I want to map a menu (tree). I have one object '
Entry' that hold one reference to his parent and a
Set<Entry> of childs. Each entries are stored in the same table named 'menu'. I use the 'parentId' column to know if it is a root entry (a root entry has a null parentId).
It seems to be easy to map because it is just a one to many inversed relation. But in fact it seems to be more difficult. When I try to save an
Entry the table isn't correctly bound and when I try to restore it I get an exeception.
Can you please help me ?
Hibernate version: 3.2.5
Mapping documents:Quote:
<hibernate-mapping>
<class name="xx.yy.Entry" table="menu">
<id name="id" type="java.lang.Integer">
<generator class="native" />
</id>
<property name="libelle" />
<many-to-one
name="parent"
class="xx.yy.Entry"
column="parentId"
lazy="false" />
<set name="childs" inverse="true" lazy="false">
<key column="id" />
<one-to-many class="xx.yy.Entry"/>
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Entry home = new Entry("home.html", "home");
session.save(home);
Entry cotations = new Entry("cotations.html", "cotations");
session.save(cotations);
Entry ch = new Entry("Boucherie","boucherie");
session.save(ch);
cotations.addChild(ch);
session.update(cotations);
Full stack trace of any exception that occurs:Quote:
Exception in thread "main" org.hibernate.PropertyAccessException: Exception occurred inside setter of xx.yy.Entry.childs
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:65)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:337)
at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:200)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3566)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
at org.hibernate.loader.Loader.doQuery(Loader.java:729)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at xx.yy.zz.hibernate.SpringHibernateMenuDao.getRoots(SpringHibernateMenuDao.java:53)
at xx.yy.zz.Test.main(Test.java:55)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:42)
... 15 more
Caused by: org.hibernate.PropertyAccessException: Exception occurred inside setter of xx.yy.Entry.parent
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:65)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:337)
at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:200)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3566)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
at org.hibernate.loader.Loader.doQuery(Loader.java:729)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1994)
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.PersistentSet.iterator(PersistentSet.java:163)
at xx.yy.Entry.setChilds(Entry.java:205)
... 20 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:42)
... 36 more
Caused by: org.hibernate.LazyInitializationException: illegal access to loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:341)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.AbstractPersistentCollection.readElementExistence(AbstractPersistentCollection.java:142)
at org.hibernate.collection.PersistentSet.add(PersistentSet.java:187)
at xx.yy.Entry.addChild(Entry.java:196)
at xx.yy.Entry.setParent(Entry.java:158)
... 41 more
Name and version of the database you are using:PostgreSQL 8.2
The generated SQL (show_sql=true):Quote:
2008-06-10 08:56:32,750 DEBUG [org.hibernate.SQL] -
/* criteria query */ select
this_.id as id0_0_,
this_.libelle as libelle0_0_,
this_.description as descript3_0_0_,
this_.icon as icon0_0_,
this_.ordering as ordering0_0_,
this_.target as target0_0_,
this_.parentid as parentid0_0_
from
menu this_
where
this_.parentid is null
2008-06-10 08:56:32,812 DEBUG [org.hibernate.SQL] -
/* load one-to-many xx.yy.Entry.childs */ select
childs0_.parentid as parentid1_,
childs0_.id as id1_,
childs0_.id as id0_0_,
childs0_.libelle as libelle0_0_,
childs0_.description as descript3_0_0_,
childs0_.icon as icon0_0_,
childs0_.ordering as ordering0_0_,
childs0_.target as target0_0_,
childs0_.parentid as parentid0_0_
from
menu childs0_
where
childs0_.parentid=?
2008-06-10 08:56:32,859 DEBUG [org.hibernate.SQL] -
/* load one-to-many xx.yy.Entry.childs */ select
childs0_.parentid as parentid1_,
childs0_.id as id1_,
childs0_.id as id0_0_,
childs0_.libelle as libelle0_0_,
childs0_.description as descript3_0_0_,
childs0_.icon as icon0_0_,
childs0_.ordering as ordering0_0_,
childs0_.target as target0_0_,
childs0_.parentid as parentid0_0_
from
menu childs0_
where
childs0_.parentid=?
2008-06-10 08:56:32,875 DEBUG [org.hibernate.SQL] -
/* load one-to-many xx.yy.Entry.childs */ select
childs0_.parentid as parentid1_,
childs0_.id as id1_,
childs0_.id as id0_0_,
childs0_.libelle as libelle0_0_,
childs0_.description as descript3_0_0_,
childs0_.icon as icon0_0_,
childs0_.ordering as ordering0_0_,
childs0_.target as target0_0_,
childs0_.parentid as parentid0_0_
from
menu childs0_
where
childs0_.parentid=?
My table :Code:
CREATE TABLE menu (
id INTEGER PRIMARY KEY,
parentid INTEGER,
libelle CHARACTER VARYING,
CONSTRAINT menu_pk PRIMARY KEY (id),
CONSTRAINT menu_fk_menu FOREIGN KEY (parentid)
REFERENCES menu (id) ON UPDATE CASCADE ON DELETE CASCADE,
)
Thanks[/i]