-->
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.  [ 2 posts ] 
Author Message
 Post subject: Pb avec le mapping d'une list
PostPosted: Thu Apr 13, 2006 3:51 am 
Newbie

Joined: Wed Aug 10, 2005 6:26 am
Posts: 8
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...


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 5:49 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Ton mapping est errone. Voici correction ci-dessous:

Code:
<class name="TestList" table="TEST_LIST" >
  <id name="id" type="integer" column="idTestList">
    <generator class="native" />
  </id>

  <list name="listObj" cascade="all-delete-orphan">
    <key column="refTestList" not-null="true" />
    <list-index column="index"/>
    <one-to-many class="Obj" />
  </list>
</class>


Remarques:

- pas besoin de specifier table="OBJ" dans le mapping de la liste - ceci n'est utile que dans le cas des many-to-many par exemple. Dans le cas present, Hibernate sait tres bien dans quelle table cela se trouve: celle de l'entitie "Obj". Ca ne serait pas le cas si un Obj pouvait etre lie a plusieurs listes (many-to-many) - dans ce cas une table intermediaire doit etre specifiee.

- <key column... indique dans quelle colonne la reference a la liste doit etre stockee

- <list index column... indique dans quelle colonne la position de l'element dans la liste doit etre stockee (par defaut, 0 -> element listObj[0], 1 -> element listObj[1], etc) Cfr. Hibernate doc §6.2.3

(any credit?)


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

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.