Bonjour,
J'aimerais bien insérer de nouveaux éléments dans ma base de donnée Postgres, jai utilisé Hibernate pour le mapping, tout semble être correct, mais je il n'y a aucune modification sur la base de donnée.
Ci-dessous certains fichiers de mon projet
hibernate.cfg.xml
TContact.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 <?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 > <!-- local connection properties --> <property name="hibernate.connection.url">jdbc:postgresql://localhost/Base1</property> <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> <property name="hibernate.connection.username">postgres</property> <property name="hibernate.connection.password">postgres</property> <!-- property name="hibernate.connection.pool_size"></property --> <!-- dialect for DB2 --> <property name="dialect">org.hibernate.dialect.DB2Dialect</property> <property name="hibernate.show_sql">false</property> <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> </session-factory> </hibernate-configuration>
HibernateUtil.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 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="org.hibernate"> <class name="TContact" table="t_contact" > <meta attribute="sync-DAO">false</meta> <property name="Id" column="id" type="java.lang.Long" not-null="false" length="10" /> <property name="Nom" column="nom" type="string" not-null="false" /> <property name="Prenom" column="prenom" type="string" not-null="false" /> <property name="Age" column="age" type="string" not-null="false" /> </class> </hibernate-mapping>
TContact.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 package org.hibernate; import org.apache.catalina.Session; import org.hibernate.*; import org.hibernate.base.*; import org.hibernate.cfg.Configuration; import org.apache.catalina.Session; import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List; import java.lang.Object; import org.hibernate.HibernateException; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static String CONFIG_FILE_LOCATION="/hibernate.cfg.xml"; private static final ThreadLocal<Session> threadlocal=new ThreadLocal<Session>(); private static final Configuration cfg =new Configuration(); private static org.hibernate.SessionFactory sessionFactory; public static org.hibernate.Session currentSession()throws HibernateException{ Session session=(Session)threadlocal.get(); if(session==null||!((org.hibernate.Session) session).isOpen()){ if(sessionFactory==null){ try{ cfg.configure(CONFIG_FILE_LOCATION); sessionFactory=cfg.buildSessionFactory(); } catch(Exception ex){ System.err.println("Erreur"); ex.printStackTrace(); }} session=(Session) ((sessionFactory!=null)?sessionFactory.openSession():null); threadlocal.set(session);} return (org.hibernate.Session) session; } public static void CloseSession()throws HibernateException{ Session session=(Session)threadlocal.get(); threadlocal.set(null); if(session!=null){ ((SessionFactory) session).close(); } } }
BaseTContact.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 package org.hibernate; import org.hibernate.base.BaseTContact; public class TContact extends BaseTContact { private static final long serialVersionUID = 1L; /*[CONSTRUCTOR MARKER BEGIN]*/ public TContact () { super(); } /*[CONSTRUCTOR MARKER END]*/ }
Test.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
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 package org.hibernate.base; import java.io.Serializable; /** * This is an object that contains data related to the t_contact table. * Do not modify this class because it will be overwritten if the configuration file * related to this class is modified. * * @hibernate.class * table="t_contact" */ public abstract class BaseTContact implements Serializable { public static String REF = "TContact"; public static String PROP_NOM = "Nom"; public static String PROP_AGE = "Age"; public static String PROP_PRENOM = "Prenom"; public static String PROP_ID = "Id"; // constructors public BaseTContact () { initialize(); } protected void initialize () {} // fields private int id; private static java.lang.String nom; private java.lang.String prenom; private Integer age; /** * Return the value associated with the column: id */ public java.lang.Integer getId () { return id; } /** * Set the value related to the column: id * @param i the id value */ public void setId (int i) { this.id = i; } /** * Return the value associated with the column: nom */ public static java.lang.String getNom () { return nom; } /** * Set the value related to the column: nom * @param nom the nom value */ public void setNom (java.lang.String nom) { this.nom = nom; } /** * Return the value associated with the column: prenom */ public java.lang.String getPrenom () { return prenom; } /** * Set the value related to the column: prenom * @param prenom the prenom value */ public void setPrenom (java.lang.String prenom) { this.prenom = prenom; } /** * Return the value associated with the column: age */ public java.lang.Integer getAge () { return age; } /** * Set the value related to the column: age * @param integer the age value */ public void setAge (Integer integer) { this.age = integer; } public String toString () { return super.toString(); } }
Sachant que j'ai exécuter la classe Test comme Java Application et comme JavaBean, je n'ai eu aucun impact dans la base de doonnée.
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 import java.util.*; import org.hibernate.*; //import org.hibernate.classic.Session; import com.tuto.*; public class Test { public static void main(String[] args){ Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); //TContact contact = new TContact(); //contact.setNom("Dupont"); //contact.setPrenom("Jean"); //contact.setAge(new Integer(44)); //session.save(contact); TContact contact = new TContact(); contact.setNom("sun"); contact.setPrenom("yun"); contact.setAge(222); session.save(contact); session.flush() ; session.refresh(contact); tx.commit(); HibernateUtil.CloseSession(); //throws HibernateException { //} } }
Il y a t-il quelqu'un qui peux m'aider?
Merci beaucoup pour votre éventuelle aide !!
Partager