salut a tous,
voila, ca fait a peine 2 mois que j'ai laissé hibernate et j'ai deja un soucis maintenant que je reprend.
Je cherche a persister un arbre d'objets qui sont liés entre eux par des List, à partir du père (cascade).
Le pb est simple: c'est le mapping de la liste (unidirectionnel) qui pose probleme... quand je passe avec un Set, ca marche bien, mais malheureusement, je n'ai pas le droit de modifier les objets à persister, et donc je dois utiliser les list. le pb avec les listes, c'est qu'il faut préciser un index, donc moi je précise la clé primaire de chaque objet de la collection. J'ai reproduit un exemple aec 2 Objets ce qu'il ya de + simple...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public class Obj { private Integer id; private String value; public Obj(){} public Obj(String value){ setValue(value); } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public class TestList { public TestList(){} private Integer id; private List<Obj> listObj; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public List<Obj> getListObj() { return listObj; } public void setListObj(List<Obj> listObj) { this.listObj = listObj; } public boolean addObj(Obj o){ if (getListObj() == null ) setListObj(new ArrayList<Obj>() ); return getListObj().add(o); } public boolean removeObj(Obj o){ if (getListObj() != null ) return getListObj().remove(o); else return false; } }
Obj.hbm.xml
<hibernate-mapping package="fr.XXX.test.bo">
<class name="Obj" table="OBJ" >
<id name="id" type="integer" column="idObj">
<generator class="native" />
</id>
<property name="value" type="string"
not-null="false" length="100" />
</class>
</hibernate-mapping>
TestList.hbm.xml
<hibernate-mapping package="fr.XXX.test.bo">
<class name="TestList" table="TEST_LIST" >
<id name="id" type="integer" column="idTestList">
<generator class="native" />
</id>
<list name="listObj" table="OBJ" cascade="all">
<key column="refTestList" not-null="true" />
<list-index column="idObj"/>
<one-to-many class="Obj" />
</list>
</class>
</hibernate-mapping>
A partir de la, je génère le schéma relationnel suivant:
create table OBJ (
idObj integer not null auto_increment,
value varchar(100),
refTestList integer not null,
primary key (idObj)
) type=InnoDB;
create table TEST_LIST (
idTestList integer not null auto_increment,
primary key (idTestList)
) type=InnoDB;
alter table OBJ
add index FK130D7705FF3F9 (refTestList),
add constraint FK130D7705FF3F9
foreign key (refTestList)
references TEST_LIST (idTestList);
Une classe pour tester (la dao utilise spring pour injecter la sessionFactory et ca marche bien):
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ctx = new FileSystemXmlApplicationContext("/src/appContext.xml"); ListDAO dao = (ListDAO) ctx.getBean("testDAO"); TestList lst = new TestList(); for (Integer i=0 ; i<100 ; i++){ Obj o = new Obj("value --> " + i.toString()); lst.addObj(o); } dao.create(lst); }
L'erreur qui apparait:
J'ai tout essayé, en rajoutant inverse="true", en changeant la définition de la clé parorg.hibernate.MappingException: Repeated column in mapping for entity: fr.XXXX.test.bo.Obj column: idObj (should be mapped with insert="false" update="false")
la il me sort que refTestList din't have a default value...<list name="testcase" table="OBJ" cascade="all" inverse="true">
<key>
<column name="refTestList" not-null="true" />
</key>
<list-index column="idTestCase"/>
<one-to-many class="TestcaseType" />
</list>
si vous voyez ou est l'erreur...
merci d'avance
Partager