Salam,
Lorsaue j'utilise la methode beginTransaction() la table que je dois explorer se vide !!!
lorsque je debugue je remarque qu'une fois cette ligne est executé la table devient vide :
j'ai tester le tuto proposer par hibernate.org
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 HibernateUtil.getSessionFactory().getCurrentSession() .beginTransaction();
voila mon code :
Faico.java :
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 package test; import java.util.*; public class Faico implements java.io.Serializable { public String var1; public String var2; public String getVar1() { return var1; } public void setVar1(String var1) { this.var1 = var1; } public String getVar2() { return var2; } public void setVar2(String var2) { this.var2 = var2; } }
et enfin EventManager.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 package util; import org.hibernate.*; import org.hibernate.cfg.*; import org.hibernate.cfg.Configuration; public class HibernateUtil { public static final SessionFactory sessionFactory; static { try { // Création de la SessionFactory à partir de hibernate.cfg.xml sessionFactory = new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static final ThreadLocal session = new ThreadLocal(); public static SessionFactory getSessionFactory() { return sessionFactory; } }
j'ai oublier le fichier de 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
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 org.hibernate.Session; import event.Faico; import java.util.Date; import java.util.Iterator; import java.util.List; import java.io.*; import util.HibernateUtil; public class EventManager { public static void main(String[] args) { EventManager mgr = new EventManager(); mgr.listerElements(); HibernateUtil.getSessionFactory().close(); } public void listerElements() { HibernateUtil.getSessionFactory().getCurrentSession() .beginTransaction(); List result = HibernateUtil.getSessionFactory().getCurrentSession() .createCriteria(Faico.class).list(); System.out.println(" sizeof = "+ result.size()); try { System.out.println("---" + result.size()); Iterator it = result.iterator(); while (it.hasNext()) { Faico event = (Faico) it.next(); System.out.println(("> " + event.getVar2() ) ); } } catch (Exception e) { e.printStackTrace(); } } }
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 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">org.postgresql.Driver</property> <property name="connection.url">jdbc:postgresql://localhost:5432/liferay</property> <property name="connection.username">postgres</property> <property name="connection.password">postgres</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <mapping resource="event/faico.hbm.xml"/> </session-factory> </hibernate-configuration>
Partager