-->
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.  [ 1 post ] 
Author Message
 Post subject: [Mapping] Relation père fils sur une même table.
PostPosted: Mon Jun 09, 2008 2:10 pm 
Beginner
Beginner

Joined: Fri May 23, 2008 4:37 am
Posts: 25
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:
Pas d'erreurs

Name and version of the database you are using:
PostgreSQL 8.2

The generated SQL (show_sql=true):
Quote:
2008-06-09 10:57:53,984 DEBUG [org.hibernate.SQL] -
select
nextval ('hibernate_sequence')
2008-06-09 10:57:54,046 DEBUG [org.hibernate.SQL] -
/* insert xx.yy.Entry
*/ insert
into
menu
(libelle, description, icon, ordering, target, parentId, id)
values
(?, ?, ?, ?, ?, ?, ?)
2008-06-09 10:57:54,062 DEBUG [org.hibernate.SQL] -
select
nextval ('hibernate_sequence')
2008-06-09 10:57:54,062 DEBUG [org.hibernate.SQL] -
/* insert xx.yy.Entry
*/ insert
into
menu
(libelle, description, icon, ordering, target, parentId, id)
values
(?, ?, ?, ?, ?, ?, ?)
2008-06-09 10:57:54,093 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-09 10:57:54,093 DEBUG [org.hibernate.SQL] -
/* load one-to-many xx.yy.Entry.childs */ select
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_.id=?
2008-06-09 10:57:54,140 DEBUG [org.hibernate.SQL] -
/* load one-to-many xx.yy.Entry.childs */ select
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_.id=?
2008-06-09 10:57:54,140 DEBUG [org.hibernate.SQL] -
/* load one-to-many xx.yy.Entry.childs */ select
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_.id=?
2008-06-09 10:57:54,140 DEBUG [org.hibernate.SQL] -
/* load one-to-many xx.yy.Entry.childs */ select
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_.id=?
2008-06-09 10:57:54,156 DEBUG [org.hibernate.SQL] -
/* load one-to-many xx.yy.Entry.childs */ select
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_.id=?
2008-06-09 10:57:54,156 DEBUG [org.hibernate.SQL] -
/* load one-to-many xx.yy.Entry.childs */ select
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_.id=?
2008-06-09 10:57:54,156 DEBUG [org.hibernate.SQL] -
/* load one-to-many xx.yy.Entry.childs */ select
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_.id=?
2008-06-09 10:57:54,156 DEBUG [org.hibernate.SQL] -
/* load one-to-many xx.yy.Entry.childs */ select
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_.id=?
2008-06-09 10:57:54,156 DEBUG [org.hibernate.SQL] -
/* load one-to-many xx.yy.Entry.childs */ select
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_.id=?
2008-06-09 10:57:54,156 DEBUG [org.hibernate.SQL] -
/* load one-to-many xx.yy.Entry.childs */ select
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_.id=?


Mon problème est le suivant. J'utilise un table menu pour stocker un menu (arborescence). J'utilise la colonne 'parentId' pour récupèrer les noeuds enfants et les noeuds racines qui sont identifiés par un 'parentId' null.
Le "problème" est que je suis sur une seule table qui contiens à la fois les parents et les enfants.
Lorsque je tente de sauver un parent ou un enfant, tout se passe bien mais ma table est mal remplie et à la restauration je ne récupère qu'un seul enfant qui est l'objet lui-même (le parent est l'enfant !!).

Ma 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,
)
INSERT INTO menu (id, parentid, libelle) VALUES (1, null, 'Racine1 sans enfants');
INSERT INTO menu (id, parentid, libelle) VALUES (2, null, 'Racine2 avec enfants');
INSERT INTO menu (id, parentid, libelle) VALUES (3, 2, 'Enfant1 de Racine2');
INSERT INTO menu (id, parentid, libelle) VALUES (4, 2, 'Enfant2 de Racine2');
INSERT INTO menu (id, parentid, libelle) VALUES (5, 4, 'Enfant1 de Enfant2 de  Racine2');


L'un d'entre vous à t'il déja réalisé ce genre de mapping et pourrais t'il m'aider ?

Merci


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

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.