[Mapping] probleme de compréhension
Bon à tous!
voici mon probleme:
j'ai 3 classes : A
Code:
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
| package test;
import java.io.Serializable;
public class A implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
private int _jour = 1;
private String _nom = null;
private B _b = null;
public B getB()
{
return _b;
}
public void setB(B b)
{
_b = b;
}
public int getJour()
{
return _jour;
}
public void setJour(int jour)
{
_jour = jour;
}
public String getNom()
{
return _nom;
}
public void setNom(String nom)
{
_nom = nom;
}
} |
La classe B
Code:
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
|
package test;
import java.io.Serializable;
public class B implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
private long _id = 0;
private int _val1 = 0;
private double _val2 = 0;
public int getVal1()
{
return _val1;
}
public void setVal1(int val1)
{
_val1 = val1;
}
public double getVal2()
{
return _val2;
}
public void setVal2(double val2)
{
_val2 = val2;
}
public long getId()
{
return _id;
}
public void setId(long id)
{
_id = id;
}
public boolean equals(Object object)
{
if (object instanceof B)
{
B b = (B) object;
if (_id == b.getId() && _val1 == b.getVal1() && _val2 == b.getVal2())
return true;
return false;
}
else
return super.equals(object);
}
public int hashCode()
{
int h = 0;
h = (int) ((127 * h) + _id);
h = (127 * h) + _val1;
h = (int) ((127 * h) + _val2);
return h;
}
} |
la classe C
Code:
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
|
package test;
public class C
{
private String _alpha1 = null;
private String _alpha2 = null;
private B _b = null;
public B getB()
{
return _b;
}
public void setB(B b)
{
_b = b;
}
public String getAlpha1()
{
return _alpha1;
}
public void setAlpha1(String alpha1)
{
_alpha1 = alpha1;
}
public String getAlpha2()
{
return _alpha2;
}
public void setAlpha2(String alpha2)
{
_alpha2 = alpha2;
}
} |
Mon programme de test:
Code:
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
| import org.hibernate.Session;
import org.hibernate.Transaction;
import test.A;
import test.B;
import test.C;
public class Main
{
/**
* @param args
*/
public static void main(String[] args)
{
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
A a1 = getA("a1", 15, 10, 12.3);
A a2 = getA("a2", 16, 10, 10.1);
C c1 = getC("c1", "c2", 10, 10.1);
session.saveOrUpdate(a1);
session.saveOrUpdate(a2);
session.saveOrUpdate(c1);
//*/
/*
A a = (A) session.load(A.class, new String("a2"));
session.delete(a);
//*/
tx.commit();
HibernateUtil.closeSession();
}
private static C getC(String alpha1, String alpha2, int val1, double val2)
{
C a = new C();
a.setAlpha1(alpha1);
a.setAlpha2(alpha2);
B b = new B();
b.setVal1(val1);
b.setVal2(val2);
a.setB(b);
return a;
}
private static A getA(String n, int jour, int val1, double val2)
{
A a = new A();
a.setJour(jour);
a.setNom(n);
B b = new B();
b.setVal1(val1);
b.setVal2(val2);
a.setB(b);
return a;
}
} |
Mon fichier hbm.xml
Code:
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
| <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="test">
<class name="A" table="A">
<id name="nom" type="string">
<column name="NOM" sql-type="char(6)" not-null="true"/>
<generator class="assigned"/>
</id>
<property name="jour" column="JOUR" type="integer"/>
<many-to-one name="b"
column="B_ID"
unique="true"
not-null="true"
lazy="false"
fetch="join"
cascade="save-update,delete"/>
</class>
<class name="B" table="B">
<id name="id" column="B_ID">
<generator class="increment"/>
</id>
<property name="val1" column="VAL1" type="integer"/>
<property name="val2" column="VAL2" type="double"/>
</class>
<class name="C" table="C">
<id name="alpha1" type="string">
<column name="ALPHA1" sql-type="char(6)" not-null="true"/>
<generator class="assigned"/>
</id>
<property name="alpha2" column="ALPHA2" type="string"/>
<many-to-one name="b"
column="B_ID"
unique="true"
not-null="true"
lazy="false"
fetch="join"
cascade="save-update,delete"/>
</class>
</hibernate-mapping> |
et j'utilise hibernate pour les stocké en base.
Or j'ai un pb avec le mapping, j'aimerais autant que possible que quand je modifie A:
qu'hibernate ne me régère pas un nouvelle element B, alors qu'il modifie seulement l'élément A.
Je developpe un peu pour bien me faire comprendre:
imaginons je lance 2 fois d'affiler mon programme et que je regarde en base
j'ai bien 2 ligne dans la table A
1 ligne dans la table C
et 6!!! lignes dans la table B.
quelqu'un peu m'expliquer pourquois? et/ ou comment remedier a ce probleme, j'aimerais qu'hibernate ne modifie les éléments B rataché à A ou C, pas qu'il en crée de nouveau.
d'avance merci