Bonjour,
J'essaye de mettre en place hibernate mais j'ai des problèmes.
J'ai actuellement deux classes, Configuration et Application.
Dans ma classe Application j'ai une liste de Configuration.
Lorsque j'insert une Application ou une Configuration ça marche et l'update aussi, par contre lorsque je souhaite supprimer une application je veux que la liste de configuration soit supprimer.
Mais j'obtiens l'erreur suivante :
Je comprend l'erreur mais je n'ai aucune idée de comment y remédier.a different object with the same identifier value was already associated with the session: [ApplicationBO#3]
HibernateUtil
Requêtes
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 package fr.service; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { return new Configuration().configure("hibernate-meteo.cfg.xml").buildSessionFactory(); } catch (Throwable ex) { System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } public static void shutdown() { getSessionFactory().close(); } }
Application
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 public List<?> getListObject(String className, Session se) { List<?> l = null; se.beginTransaction(); try { Query q = se.createQuery("from " + className); l = q.list(); } catch (HibernateException he) { throw he; } catch (RuntimeException ex) { throw ex; } return l; } public void deleteObject(Object o, Session se) { Transaction tx = se.beginTransaction(); try { se.delete(o); tx.commit(); } catch (Exception ex) { tx.rollback(); ex.printStackTrace(); } } public boolean saveObject(Object objet, Session se) throws StaleStateException { boolean result = false; Transaction tx = null; try { tx = se.beginTransaction(); se.saveOrUpdate(objet); tx.commit(); result = true; } catch (StaleStateException sse) { sse.printStackTrace(); tx.rollback(); se.clear(); throw sse; } catch (HibernateException he) { tx.rollback(); throw he; }
Configuration
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-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping auto-import="true"> <class lazy="false" name="ApplicationBO" table="application"> <id name="id" column="id"> <generator class="increment" /> </id> <property name="libelle" column="libelle" type="string" /> <property name="etat" column="etat" type="boolean" /> <list name="lstConfApp" cascade="all" inverse="true"> <key column="idApplication" /> <list-index column="id" /> <one-to-many class="ConfigurationApplicationBO" /> </list> </class> </hibernate-mapping>
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 <?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 auto-import="true"> <class lazy="false" name="ConfigurationApplicationBO" table="configurationapplication"> <id name="id" column="id"> <generator class="increment" /> </id> <property name="hostID" column="host_object_id" type="int" /> <property name="serviceID" column="service_object_id" type="int" /> <property name="etat" column="etat" type="boolean" /> <property name="libelle" column="libelle" type="text" /> <many-to-one name="Application" column="idApplication" cascade="all" /> </class> </hibernate-mapping>
Partager