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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
TableA.hbm.xml :
<hibernate-mapping>
<class name="TableA" table="TABLE_A" schema="TEST">
<id name="idA" type="big_decimal">
<column name="ID_A" precision="22" scale="0" />
<generator class="assigned" />
</id>
<property name="texteA" type="string">
<column name="TEXTE_A" length="100" />
</property>
<set name="tableBs" inverse="true" table="ASSOCIATION">
<key>
<column name="ID_A" precision="22" scale="0" not-null="true" />
</key>
<many-to-many entity-name="TableB">
<column name="ID_B" precision="22" scale="0" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
TableB.hbm.xml :
<hibernate-mapping>
<class name="TableB" table="TABLE_B" schema="TEST">
<id name="idB" type="big_decimal">
<column name="ID_B" precision="22" scale="0" />
<generator class="assigned" />
</id>
<property name="texteB" type="string">
<column name="TEXTE_B" length="50" />
</property>
<set name="tableAs" inverse="false" table="ASSOCIATION">
<key>
<column name="ID_B" precision="22" scale="0" not-null="true" />
</key>
<many-to-many entity-name="TableA">
<column name="ID_A" precision="22" scale="0" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
TableA.java :
public class TableA implements java.io.Serializable {
private BigDecimal idA;
private String texteA;
private Set tableBs = new HashSet(0);
public TableA() {
}
public TableA(BigDecimal idA) {
this.idA = idA;
}
public TableA(BigDecimal idA, String texteA, Set tableBs) {
this.idA = idA;
this.texteA = texteA;
this.tableBs = tableBs;
}
public BigDecimal getIdA() {
return this.idA;
}
public void setIdA(BigDecimal idA) {
this.idA = idA;
}
public String getTexteA() {
return this.texteA;
}
public void setTexteA(String texteA) {
this.texteA = texteA;
}
public Set getTableBs() {
return this.tableBs;
}
public void setTableBs(Set tableBs) {
this.tableBs = tableBs;
}
}
TableB.java :
public class TableB implements java.io.Serializable {
private BigDecimal idB;
private String texteB;
private Set tableAs = new HashSet(0);
public TableB() {
}
public TableB(BigDecimal idB) {
this.idB = idB;
}
public TableB(BigDecimal idB, String texteB, Set tableAs) {
this.idB = idB;
this.texteB = texteB;
this.tableAs = tableAs;
}
public BigDecimal getIdB() {
return this.idB;
}
public void setIdB(BigDecimal idB) {
this.idB = idB;
}
public String getTexteB() {
return this.texteB;
}
public void setTexteB(String texteB) {
this.texteB = texteB;
}
public Set getTableAs() {
return this.tableAs;
}
public void setTableAs(Set tableAs) {
this.tableAs = tableAs;
}
}
et la class Le code pour l'insertion un nouvel enregistrement dans table A:
TableA tableA=new TableA();
TableB tableB=new TableB();
TableB tableB1=new TableB();
TableB tableB2=new TableB();
Set<TableB> tableBs=new HashSet<TableB>();
tableB.setIdB(BigDecimal.valueOf(1));
tableB.setTexteB("B1");
tableBs.add(tableB);
tableB1.setIdB(BigDecimal.valueOf(2));
tableB1.setTexteB("Bb2");
tableBs.add(tableB1);
tableB2.setIdB(BigDecimal.valueOf(3));
tableB2.setTexteB("Bb3");
tableBs.add(tableB2);
tableA.setIdA(BigDecimal.valueOf(4));
tableA.setTexteA("A4");
tableA.setTableBs(tableBs);
Session session= null;
Transaction tx=null;
try{
session= HibernateUtil.openSession();
tx=session.beginTransaction();
session.save(tableA);
tx.commit();
session.flush();
}catch(Exception e){
if(tx!=null)
tx.rollback();
}finally{
session.close();
} |
Partager