Bonjour à tous,

J'ai trouvé beaucoup d'informations au sujet du mapping d'une collection mais je n'arrive pas à la faire fonctionner car je ne comprends pas tout.

Admettons que je voudrais mapper des objets représentant des chats. Ces chats ont donc des attributs simples que j'arrive à mapper (un nom, un id, un sexe...)

Donc à cette étape ca fonctionne. Chaque attribut est représenté par une colonne dans la table correspondante.

Par contre, ca se corse quand je veux rajouter une liste ou un ensemble d'amis à un chat. Comment faire pour mapper cette liste ???

Voici mes fichiers :

Cat.hbm.xml :
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 <class name="org.hibernate.examples.testAFC.Cat" table="cat">
  <!-- A 32 hex character is our surrogate key. It's automatically
            generated by Hibernate with the UUID pattern. -->
  <id name="id" type="string">
   <column name="cat_id" not-null="true" sql-type="char(32)"/>
   <generator class="uuid.hex"/>
  </id>
  <!-- A cat has to have a name, but it shouldn' be too long. -->
  <property lazy="false" name="name">
   <column length="16" name="name" not-null="true"/>
  </property>
  <property lazy="false" name="sex"/>
  <property lazy="false" name="weight"/>
  <set name="friends" sort="unsorted">
	<key column="friend_id"/>
   	<one-to-many class="org.hibernate.examples.testAFC.Cat"/>
  </set>
 </class>
</hibernate-mapping>
hibernate.cfg.xml :
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
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
 
    <session-factory>
 
        <property name="connection.datasource">java:comp/env/jdbc/testAFC</property>
        <property name="show_sql">false</property>
        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
 
        <!-- Mapping files -->
        <mapping resource="Cat.hbm.xml"/>
 
    </session-factory>
 
</hibernate-configuration>
Cat.java :
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package org.hibernate.examples.testAFC;
 
import java.util.HashSet;
import java.util.Set;
 
import org.hibernate.Session;
import org.hibernate.Transaction;
 
public class Cat {
 
    private String id;
    private String name;
    private char sex;
    private float weight;
    private Set friends;
 
    public Cat() {
    	friends = new HashSet();
    }
 
    public Set getFriends() {
    	return friends;
    }
 
    public void setFriends(HashSet set) {
    	friends = set;
    }
 
    public String getId() {
        return id;
    }
 
    private void setId(String id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public char getSex() {
    	Session session = HibernateUtil.currentSession();
    	Transaction tx = session.beginTransaction();
 
    	this.setName("Princess");
    	this.setSex('F');
    	this.setWeight(7.4f);
 
    	for (int i=0;i<5;i++) {
    		Cat cat = new Cat();
    		cat.setName("cat"+Integer.toString(i));
    		cat.setSex('F');
    		cat.setWeight(7.6f);
                                session.save(cat);
    	}
 
    	session.save(this);
 
    	tx.commit();
    	HibernateUtil.closeSession();
 
        return sex;
    }
 
    public void setSex(char sex) {
        this.sex = sex;
    }
 
    public float getWeight() {
        return weight;
    }
 
    public void setWeight(float weight) {
        this.weight = weight;
    }
}
Alors je fais le mapping dans la méthode getSex() car il s'agit d'un bean utilisé dans une page JSF et qui appelle cette méthode...

Ce que je ne comprends pas, c'est quelle colonne je dois rajouter dans ma table pour stocker ma liste ? Et donc quel est le lien avec ce passage de mon fichier Cat.hbm.xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
<set name="friends" sort="unsorted">
	<key column="friend_id"/>
   	<one-to-many class="org.hibernate.examples.testAFC.Cat"/>
  </set>

Merci d'avance
@+
Jorus