Mmmm , dans le cas de localhost que devient le chemin? ...
Puis étonnant dans les logs il marque bien qu'il est connecté
Version imprimable
Mmmm , dans le cas de localhost que devient le chemin? ...
Puis étonnant dans les logs il marque bien qu'il est connecté
Car pour le moment, en remplacant :
:file:database/defense
par:
:file://localhost/defense
il aime pas.
Il me dit qu' il trouve pas la DB mais par contre il me dit que la BD est lock MAIS que le fichier defense.lck existe pas...
debile :s
/me pense qu'il n arrive jamais a faire ce satané insert :(
Mais a premiere vue, le fait de passer par ceci :
est correct.Code:
1
2<property name="hibernate.connection.url">jdbc:hsqldb:file:database/defense</property>
Car je lance l'appli. il ne me met aucune erreur, il trouve bien la DB etc ...
En faisant:
J' obtiens l'erreur suivante:Code:
1
2<property name="hibernate.connection.url">jdbc:hsqldb:file:databaseBROL/defense</property>
Preuve, selon moi que dans le premier cas c'est OK.Code:
1
2
3
4
5
6 Hibernate: insert into DEBITS (debit, id) values (?, ?) 09:21:24,961 WARN JDBCExceptionReporter:77 - SQL Error: -22, SQLState: S0002 09:21:24,962 ERROR JDBCExceptionReporter:78 - Table not found in statement [insert into DEBITS (debit, id) values (?, ?)] 09:21:24,964 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session org.hibernate.exception.SQLGrammarException: could not insert: [hibertest.Debit]
J' en conclus donc un probleme de cache comme expliqué dans les posts précédents, où HSQLDB a besoin d'être stoppé par un SHUTDOWN pour ecrire ce qu'il y a dans le cache (le nouveau tuple dû au insert) dans le fichier sur disque. Car là je vois plus que ca ...
Pour commencer avec Hibernate, il faudrait ne pas cumuler les problèmes.
Je te suggère donc d'utiliser une base mySQL pour le début parce qu'on dirait que la base HSQLDB pose problème
Regarde le code, c'est minimaliste, mais ça fonctionne (à toi d'adapter les noms de pojo etc...)
La config HibernateCode:
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 package hibernate.utilities; import hibernate.objects.AlUtilisateurInfo; import org.hibernate.Session; public class TestHibernate { public static void main(String[] args) { try { Session session = HibernateUtil.currentSession(); AlUtilisateurInfo user = new AlUtilisateurInfo(); user.setUtiNom("UN NOM"); user.setUtiLibelle("le libellé associé"); session.beginTransaction(); session.save(user); session.getTransaction().commit(); } catch (Throwable t) { System.out.println("Erreur : " + t.getMessage()); } finally { HibernateUtil.closeSession(); } } } La classe HibernateUtil ================= package hibernate.utilities; import org.hibernate.*; import org.hibernate.cfg.*; public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { sessionFactory = new Configuration().configure().buildSessionFactory(); } catch (HibernateException ex) { throw new RuntimeException("Problème de configuration : " + ex.getMessage(), ex); } } public static final ThreadLocal session = new ThreadLocal(); /** * Récupération de la session Hibernate */ public static Session currentSession() throws HibernateException { Session s = (Session) session.get(); // Ouvre une nouvelle Session, si ce Thread n'en a aucune if (s == null) { s = sessionFactory.openSession(); session.set(s); } return s; } /** * Fermeture de la session Hibernate */ public static void closeSession() throws HibernateException { Session s = (Session) session.get(); if ( s == null ) return ; session.set(null); s.close(); } }
Chez moi, ça fonctionne (avec mySQL) et chez toi ?Code:
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 <?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 name="GTC"> <property name="hibernate.bytecode.use_reflection_optimizer">false</property> <property name="hibernate.cglib.use_reflection_optimizer">true</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///gtc</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">ob</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">false</property> <mapping resource="hibernate/objects/AlUtilisateurInfo.hbm.xml" /> </session-factory> </hibernate-configuration> le mapping ======== <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 18 sept. 2006 10:14:34 by Hibernate Tools 3.1.0.beta5 --> <hibernate-mapping> <class name="hibernate.objects.AlUtilisateurInfo" table="al_utilisateur_info"> <comment></comment> <id name="utiNom" type="string"> <column name="uti_nom" length="8" /> <generator class="assigned" /> </id> <property name="utiLibelle" type="string"> <column name="uti_libelle" length="80"> <comment></comment> </column> </property> </class> </hibernate-mapping>
Résultat
Code:
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 init: deps-jar: Copying 1 file to /home/bardack/workspace/TestHibernate/build/classes compile: run: 10:01:59,599 INFO Environment:509 - Hibernate 3.2.2 10:01:59,606 INFO Environment:542 - hibernate.properties not found 10:01:59,610 INFO Environment:676 - Bytecode provider name : cglib 10:01:59,615 INFO Environment:593 - using JDK 1.4 java.sql.Timestamp handling 10:01:59,680 INFO Configuration:1426 - configuring from resource: /hibernate.cfg.xml 10:01:59,682 INFO Configuration:1403 - Configuration resource: /hibernate.cfg.xml 10:01:59,934 WARN Environment:498 - Property [hibernate.cglib.use_reflection_optimizer] has been renamed to [hibernate.bytecode.use_reflection_optimizer]; update your properties appropriately 10:01:59,935 INFO Configuration:553 - Reading mappings from resource : mapping/AlUtilisateurInfo.hbm.xml 10:02:00,063 INFO HbmBinder:300 - Mapping class: testhibernate.AlUtilisateurInfo -> al_utilisateur_info 10:02:00,077 INFO Configuration:1541 - Configured SessionFactory: GTC 10:02:00,132 WARN Environment:498 - Property [hibernate.cglib.use_reflection_optimizer] has been renamed to [hibernate.bytecode.use_reflection_optimizer]; update your properties appropriately 10:02:00,139 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!) 10:02:00,139 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 20 10:02:00,140 INFO DriverManagerConnectionProvider:45 - autocommit mode: false 10:02:00,145 INFO DriverManagerConnectionProvider:80 - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/test 10:02:00,153 INFO DriverManagerConnectionProvider:86 - connection properties: {user=root, password=****} 10:02:00,362 INFO SettingsFactory:89 - RDBMS: MySQL, version: 5.0.32-Debian_7-log 10:02:00,363 INFO SettingsFactory:90 - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.0.4 ( $Date: 2006-10-19 17:47:48 +0200 (Thu, 19 Oct 2006) $, $Revision: 5908 $ ) 10:02:00,389 INFO Dialect:152 - Using dialect: org.hibernate.dialect.MySQLDialect 10:02:00,395 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions) 10:02:00,397 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended) 10:02:00,398 INFO SettingsFactory:143 - Automatic flush during beforeCompletion(): disabled 10:02:00,399 INFO SettingsFactory:147 - Automatic session close at end of transaction: disabled 10:02:00,399 INFO SettingsFactory:154 - JDBC batch size: 15 10:02:00,399 INFO SettingsFactory:157 - JDBC batch updates for versioned data: disabled 10:02:00,400 INFO SettingsFactory:162 - Scrollable result sets: enabled 10:02:00,401 INFO SettingsFactory:170 - JDBC3 getGeneratedKeys(): enabled 10:02:00,402 INFO SettingsFactory:178 - Connection release mode: auto 10:02:00,403 INFO SettingsFactory:202 - Maximum outer join fetch depth: 2 10:02:00,404 INFO SettingsFactory:205 - Default batch fetch size: 1 10:02:00,404 INFO SettingsFactory:209 - Generate SQL with comments: disabled 10:02:00,407 INFO SettingsFactory:213 - Order SQL updates by primary key: disabled 10:02:00,407 INFO SettingsFactory:382 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory 10:02:00,409 INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory 10:02:00,410 INFO SettingsFactory:221 - Query language substitutions: {} 10:02:00,410 INFO SettingsFactory:226 - JPA-QL strict compliance: disabled 10:02:00,410 INFO SettingsFactory:231 - Second-level cache: enabled 10:02:00,411 INFO SettingsFactory:235 - Query cache: disabled 10:02:00,411 INFO SettingsFactory:369 - Cache provider: org.hibernate.cache.NoCacheProvider 10:02:00,412 INFO SettingsFactory:250 - Optimize cache for minimal puts: disabled 10:02:00,420 INFO SettingsFactory:259 - Structured second-level cache entries: disabled 10:02:00,425 INFO SettingsFactory:286 - Statistics: disabled 10:02:00,425 INFO SettingsFactory:290 - Deleted entity synthetic identifier rollback: disabled 10:02:00,426 INFO SettingsFactory:305 - Default entity-mode: pojo 10:02:00,426 INFO SettingsFactory:309 - Named query checking : enabled 10:02:00,456 INFO SessionFactoryImpl:161 - building session factory 10:02:00,732 INFO SessionFactoryObjectFactory:86 - Factory name: GTC 10:02:00,734 INFO NamingHelper:26 - JNDI InitialContext properties:{} 10:02:00,738 WARN SessionFactoryObjectFactory:98 - Could not bind factory to JNDI javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247) at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284) at javax.naming.InitialContext.getNameParser(InitialContext.java:439) at org.hibernate.util.NamingHelper.bind(NamingHelper.java:52) at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:90) at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:306) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294) at testhibernate.HibernateUtil.<clinit>(HibernateUtil.java:15) at testhibernate.TestHibernate.main(TestHibernate.java:20)
Et encore une erreur que j'avais pas eu et que je pige pas, une ...
Hey, par contre il remplit la table ...
Marrant, quand je passe par une table ecrite de cette facon :
create table DEBIT (id INT (2), debit INT (4));
ca passe pour l'add (mais l erreur ci dessus est tjr là)
Par contre si je fais :
CREATE TABLE DEBITS (
id INT (2) NOT NULL,
debit INT (4) NOT NULL,
PRIMARY KEY (id)
);
Là il n' en veux plus ... commence a me saouler Hibernate :)
Enfin bon, a premiere vue c est bien un probleme dû à HSQLDB. Ne faisant pas le SHUTDOWN, il sauve pas ce qu'il y avait en cache , donc ...
Pour ce qui est de mon post précédent, a premiere vue ca marche sur les deux tables, mais je pense avoir lu qu'il ne fallait pas gerer les trucs du genre:
NOT NULL, PRIMARY KEY, FOREIGN KEY, etc... au niveau sql mais bien au niveau du mapping.
Me confirmez vous la chose?
Si, tu dois les gérer au niveau base de données, et mettre ton mapping en accord.
Oki, ca me semble effectivement + cohérent
Bon et bien, malgré l'erreur qui persiste, ca commence à rouler. J' arrive à faire un SELECT qui me retourne une List, etc ... pratique soit dit en passant.
Mais en ce moment me voilà contraint de bosser avec MySql, ce qui ne m'arrange pas du tout.
Si quelqu un a une idée pour :
1) l'erreur citée ci dessus
2) pouvoir utiliser Hibernate avec HSQLDB. Il est dit dans la doc. de HSQLDB d' effectuer la commande SHUTDOWN avant de fermer l'application pour enregistrer ... donc je vois pas trop
Merci d'avance pour votre aide ;)
Tiens, j'avais pas fait gaffe à un truc...
Peux-tu montrer le fichier hibernate.cfg.xml que tu utilises avec HSQLDB ?
Pour le shutdown je vais me répèter, tu peux le faire en java en appelant la méthode shutdown de la classe serveur. (C'est ce qu'il fera de toute façon en recevant l'oredre shutdown en JDBC, donc autant le faire directement en java)Citation:
Envoyé par Bardack
Et le lien vers le sujet que je t'avais filé contient la config hibernate d'un de mes projets où je l'utilise avec HSQL.
Code:
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-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property> <property name="hibernate.connection.url">jdbc:hsqldb:file:database/defense</property> <property name="hibernate.connection.username">sa</property> <property name="hibernate.connection.password"></property> <property name="dialect">org.hibernate.dialect.HSQLDialect</property> <property name="hibernate.show_sql">true</property> <mapping resource="mapping/debit.hbm.xml"/> </session-factory> </hibernate-configuration>
L' erreur que j'avais était simplement dûe a cela:
<session-factory name="DEFENSE">
dans mon hibernate.cfg.xml
Lorsque je remplace par :
<session-factory>
plus d erreur
Non, finalement il n'y a rien...
J'ai regardé sur un site un exemple avec hibernate et hsqldb
(http://www.gloegl.de/8.html)
Ils parlent bien de cette commande SHUTDOWN (ç'est vraiment naze) et qu'elle n'est pas prise en charge par Hibernate
(Je pense que l'appel directe via le server comme disait sinok est mieux)
Bref, si tu as récupéré l'objet HibernateUtil, tu n'as qu'à rajouter la fonction dans la méthode closeSession(), ça évitera de trainer ce bout partout...
Pour l'erreur JNDI, regarde si dans ton jar tu n'as pas de fichiers de propriétés qui y font référence...
A+
Il est vrai que c est un peu naze.
Mais dans mon cas, il est encore + naze d'installer un serveur SQL sur chaque machine utilisant l'appli... car bon la BD ne sert que de vérification à la cohérence
Le "naze" concernait HSQLDB, pas le fait que tu l'utilises pour les raisons que tu avais mentionnées :mouarf:
Oui tout a fait, on s etait compris ;)