Bonjour

Je debut en hibernate, j'ai donc pris un exemple que j'essaye de faire fonctionner.
Lorsque je lance l'execution par le programme de Test aucun record ne s'insere dans la Base ?
Ou es mon erreur ?
Merci
Philippe

Voici mon log
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
 
de.laliluna.example.TestExample
16:23:57,593  INFO Version:15 - Hibernate Annotations 3.4.0.GA
16:23:57,609  INFO Environment:543 - Hibernate 3.3.0.SP1
16:23:57,609  INFO Environment:576 - hibernate.properties not found
16:23:57,625  INFO Environment:709 - Bytecode provider name : javassist
16:23:57,625  INFO Environment:627 - using JDK 1.4 java.sql.Timestamp handling
16:23:57,687  INFO Version:14 - Hibernate Commons Annotations 3.1.0.GA
16:23:57,687  INFO Configuration:1460 - configuring from resource: /hibernate.cfg.xml
16:23:57,687  INFO Configuration:1437 - Configuration resource: /hibernate.cfg.xml
16:23:57,781  INFO Configuration:1575 - Configured SessionFactory: null
16:23:57,781  INFO HibernateSearchEventListenerRegister:53 - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
16:23:57,843  INFO AnnotationBinder:419 - Binding entity from annotated class: de.laliluna.example.Honey
16:23:57,875  INFO EntityBinder:422 - Bind entity de.laliluna.example.Honey on table Honey
16:23:57,921  INFO AnnotationConfiguration:369 - Hibernate Validator not found: ignoring
16:23:57,984  INFO DriverManagerConnectionProvider:64 - Using Hibernate built-in connection pool (not for production use!)
16:23:57,984  INFO DriverManagerConnectionProvider:65 - Hibernate connection pool size: 20
16:23:57,984  INFO DriverManagerConnectionProvider:68 - autocommit mode: false
16:23:57,984  INFO DriverManagerConnectionProvider:103 - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://192.168.17.4/employes
16:23:57,984  INFO DriverManagerConnectionProvider:109 - connection properties: {user=gestion, password=****}
16:23:58,468  INFO SettingsFactory:116 - RDBMS: MySQL, version: 5.1.34-community
16:23:58,468  INFO SettingsFactory:117 - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.7 ( Revision: ${svn.Revision} )
16:23:58,484  INFO Dialect:175 - Using dialect: org.hibernate.dialect.MySQLDialect
16:23:58,484  INFO TransactionFactoryFactory:62 - Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
16:23:58,484  INFO TransactionManagerLookupFactory:80 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
16:23:58,484  INFO SettingsFactory:170 - Automatic flush during beforeCompletion(): disabled
16:23:58,484  INFO SettingsFactory:174 - Automatic session close at end of transaction: disabled
16:23:58,484  INFO SettingsFactory:181 - JDBC batch size: 15
16:23:58,484  INFO SettingsFactory:184 - JDBC batch updates for versioned data: disabled
16:23:58,484  INFO SettingsFactory:189 - Scrollable result sets: enabled
16:23:58,484  INFO SettingsFactory:197 - JDBC3 getGeneratedKeys(): enabled
16:23:58,484  INFO SettingsFactory:205 - Connection release mode: auto
16:23:58,484  INFO SettingsFactory:229 - Maximum outer join fetch depth: 2
16:23:58,484  INFO SettingsFactory:232 - Default batch fetch size: 1
16:23:58,484  INFO SettingsFactory:236 - Generate SQL with comments: disabled
16:23:58,484  INFO SettingsFactory:240 - Order SQL updates by primary key: disabled
16:23:58,484  INFO SettingsFactory:244 - Order SQL inserts for batching: disabled
16:23:58,484  INFO SettingsFactory:420 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
16:23:58,484  INFO ASTQueryTranslatorFactory:47 - Using ASTQueryTranslatorFactory
16:23:58,484  INFO SettingsFactory:252 - Query language substitutions: {}
16:23:58,500  INFO SettingsFactory:257 - JPA-QL strict compliance: disabled
16:23:58,500  INFO SettingsFactory:262 - Second-level cache: enabled
16:23:58,500  INFO SettingsFactory:266 - Query cache: disabled
16:23:58,500  INFO SettingsFactory:405 - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
16:23:58,500  INFO SettingsFactory:276 - Optimize cache for minimal puts: disabled
16:23:58,500  INFO SettingsFactory:285 - Structured second-level cache entries: disabled
16:23:58,500  INFO SettingsFactory:305 - Echoing all SQL to stdout
16:23:58,500  INFO SettingsFactory:314 - Statistics: disabled
16:23:58,500  INFO SettingsFactory:318 - Deleted entity synthetic identifier rollback: disabled
16:23:58,500  INFO SettingsFactory:333 - Default entity-mode: pojo
16:23:58,500  INFO SettingsFactory:337 - Named query checking : enabled
16:23:58,609  INFO SessionFactoryImpl:187 - building session factory
16:23:58,828  INFO SessionFactoryObjectFactory:105 - Not binding factory to JNDI, no JNDI name configured
16:23:58,828  INFO SchemaExport:226 - Running hbm2ddl schema export
16:23:58,843  INFO SchemaExport:251 - exporting generated schema to database
16:23:58,859  INFO SchemaExport:268 - schema export complete
16:23:58,953 DEBUG SQL:111 - insert into Honey (name, taste) values (?, ?)
Hibernate: insert into Honey (name, taste) values (?, ?)
16:23:58,984 DEBUG SQL:111 - insert into Honey (name, taste) values (?, ?)
Hibernate: insert into Honey (name, taste) values (?, ?)
16:23:58,984 DEBUG TestExample:47 - Honey: 1 Name: forest honey Taste: very sweet
16:23:58,984 DEBUG TestExample:48 - Honey: 2 Name: country honey Taste: tasty
16:23:59,125 DEBUG SQL:111 - select honey0_.id as id0_, honey0_.name as name0_, honey0_.taste as taste0_ from Honey honey0_
Hibernate: select honey0_.id as id0_, honey0_.name as name0_, honey0_.taste as taste0_ from Honey honey0_
16:23:59,140 DEBUG TestExample:66 - Honey: 1 Name: forest honey Taste: very sweet
16:23:59,140 DEBUG TestExample:66 - Honey: 2 Name: country honey Taste: tasty
16:23:59,140 DEBUG SQL:111 - delete from Honey where id=?
Hibernate: delete from Honey where id=?
16:23:59,140 DEBUG SQL:111 - select honey0_.id as id0_, honey0_.name as name0_, honey0_.taste as taste0_ from Honey honey0_
Hibernate: select honey0_.id as id0_, honey0_.name as name0_, honey0_.taste as taste0_ from Honey honey0_
16:23:59,140 DEBUG TestExample:66 - Honey: 1 Name: forest honey Taste: very sweet
16:23:59,156 DEBUG SQL:111 - update Honey set name=?, taste=? where id=?
Hibernate: update Honey set name=?, taste=? where id=?
Voici le code de la classe de Test
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
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
 
package de.laliluna.example;
 
import java.util.Iterator;
import java.util.List;
 
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import de.laliluna.hibernate.SessionFactoryUtil;
 
public class TestExample {
 
  final static Logger logger = LoggerFactory.getLogger(TestExample.class);
 
  /**
   * @param args
   * Exemple pris sur le site http://www.laliluna.de
   * Pour les jar 
   *    Necessite dans le lib plus Jar de Hibernate
   *    Les annotations
   *    Les Entity
   *    La bonne version de log4j
   *    Le fichier ivy.xml contient le nom et les versions des jar pour les dependances (notament log4j sinon pb avec TRACE)
   *    
   * le fichier hibernate.cfg.xml contient le driver de la base de donnee (verifier la bonne database et la bonne adresse IP)
   * 
   * ne pas oublier de creer la database 'employes' et la table 'honey', les fichiers hbm.xml contiennent le mapping avec les tables
   * 
   * En version3  d'Hibernate, l'INSERT n'existe plus .. il faut passer par du SQL et plus du HQL
   * 
   * 
   *Creation de la Table Avec MySQL utilisez le script SQL suivant:
   * CREATE TABLE `honey` (  `id` int(11) NOT NULL auto_increment,  `name` varchar(250) default NULL,  `taste` varchar(250) default NULL,  PRIMARY KEY  (`id`)) ENGINE=MyISAM CHARSET=latin1
 */
 
  public static void main(String[] args) {
    Honey forestHoney = new Honey();
    forestHoney.setName("forest honey");
    forestHoney.setTaste("very sweet");
    Honey countryHoney = new Honey();
    countryHoney.setName("country honey");
    countryHoney.setTaste("tasty");
    createHoney(forestHoney);
    createHoney(countryHoney);
    // our instances have a primary key now:
    logger.debug("{}", forestHoney);
    logger.debug("{}", countryHoney);
    listHoney();
    deleteHoney(countryHoney);
    listHoney();
    forestHoney.setName("Norther Forest Honey");
    updateHoney(forestHoney);
 
  }
 
  private static void listHoney() {
    Transaction tx = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      tx = session.beginTransaction();
      List honeys = session.createQuery("select h from Honey as h")
              .list();
      for (Iterator iter = honeys.iterator(); iter.hasNext();) {
        Honey element = (Honey) iter.next();
        logger.debug("{}", element);
      }
      tx.commit();
    } catch (RuntimeException e) {
      if (tx != null && tx.isActive()) {
        try {
// Second try catch as the rollback could fail as well
          tx.rollback();
        } catch (HibernateException e1) {
          logger.debug("Error rolling back transaction");
        }
// throw again the first exception
        throw e;
      }
 
 
    }
  }
 
  private static void deleteHoney(Honey honey) {
    Transaction tx = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      tx = session.beginTransaction();
      session.delete(honey);
      tx.commit();
    } catch (RuntimeException e) {
      if (tx != null && tx.isActive()) {
        try {
// Second try catch as the rollback could fail as well
          tx.rollback();
        } catch (HibernateException e1) {
          logger.debug("Error rolling back transaction");
        }
// throw again the first exception
        throw e;
      }
    }
  }
 
  private static void createHoney(Honey honey) {
    Transaction tx = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      tx = session.beginTransaction();
      session.save(honey);
      tx.commit(); // le commit est bien fait ICI
    } catch (RuntimeException e) {
      if (tx != null && tx.isActive()) {
        try {
// Second try catch as the rollback could fail as well
          tx.rollback();
        } catch (HibernateException e1) {
          logger.debug("Error rolling back transaction");
        }
// throw again the first exception
        throw e;
      }
    }
  }
 
   private static void updateHoney(Honey honey) {
    Transaction tx = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      tx = session.beginTransaction();
      session.update(honey);
      tx.commit();
    } catch (RuntimeException e) {
      if (tx != null && tx.isActive()) {
        try {
// Second try catch as the rollback could fail as well
          tx.rollback();
        } catch (HibernateException e1) {
          logger.debug("Error rolling back transaction");
        }
// throw again the first exception
        throw e;
      }
    }
  }
}