salut a tous, j'utilise hibernate3 avec mysql5
voila, ca fait a peine 2 mois que j'ai laissé hibernate et j'ai deja un soucis maintenant que je reprend.
Le pb est simple avec 2 Objets ce qu'il ya de + simple: c'est le mapping de la liste (unidirectionnel) qui pose probleme...
Code:
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:
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
Quote:
<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
Quote:
<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:
Quote:
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:
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:
Quote:
org.hibernate.MappingException: Repeated column in mapping for entity: fr.XXXX.test.bo.Obj column: idObj (should be mapped with insert="false" update="false")
J'ai tout essayé, en rajoutant inverse="true", en changeant la définition de la clé par
Quote:
<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>
la il me sort que refTestList din't have a default value...
si vous voyez ou est l'erreur...