the_frogkiller | bonjour,
je développe une application en Swing qui doit stocker les données associées dans une hsqlDB en local. Cela marche très bien.
mon problèmes est que chaque utilisateur doit avoir sa petite base de donnée de sauvegarde dont la localisation est configurable par l'utilisateur.
donc ma question est: comment indiquer l'url de la BD à hibernate quand elle est configurable au niveau applicatif?
le petit bout de code du main de test:
Code :
- SessionFactory sessionFactory = null;
- try {
- // Création de la SessionFactory à partir de hibernate.cfg.xml
- Configuration cfg = new Configuration().configure();
- String url = "jdbc:hsqldb:file:G:\\java\\tests\\testDB2\\MyDB.userDB";
- cfg.getProperties().setProperty("connection.url", url);
-
- sessionFactory = cfg.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);
- }
- Session session = sessionFactory.openSession();
- Transaction transaction = session.beginTransaction();
|
mon hibernate.cfg.xml :
Code :
- <?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.hsqldb.jdbcDriver</property>
- <property name="connection.url">jdbc:hsqldb:file:G:\java\tests\testDB\CompteDB.monCompte</property>
- <property name="connection.username">sa</property>
- <property name="connection.password"></property>
- <!-- JDBC connection pool (use the built-in) -->
- <property name="connection.pool_size">1</property>
- <!-- SQL dialect -->
- <property name="dialect">org.hibernate.dialect.HSQLDialect</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="appli/Compte.hbm.xml"/>
- </session-factory>
|
et enfin les logs :
Code :
- 13:44:05,951 INFO Environment:456 - Hibernate 3.0rc1
- 13:44:05,955 INFO Environment:469 - hibernate.properties not found
- 13:44:05,959 INFO Environment:502 - using CGLIB reflection optimizer
- 13:44:05,961 INFO Environment:532 - using JDK 1.4 java.sql.Timestamp handling
- 13:44:05,963 INFO Configuration:1228 - configuring from resource: /hibernate.cfg.xml
- 13:44:05,964 INFO Configuration:1199 - Configuration resource: /hibernate.cfg.xml
- 13:44:06,110 INFO Configuration:439 - Mapping resource: appli/Compte.hbm.xml
- 13:44:06,286 INFO HbmBinder:256 - Mapping class: appli.Compte -> COMPTE
- 13:44:06,331 INFO Configuration:1340 - Configured SessionFactory: null
- 13:44:16,778 INFO Configuration:844 - processing extends queue
- 13:44:16,779 INFO Configuration:848 - processing collection mappings
- 13:44:16,779 INFO Configuration:857 - processing association property references
- 13:44:16,779 INFO Configuration:884 - processing foreign key constraints
- 13:44:16,829 INFO Dialect:89 - Using dialect: org.hibernate.dialect.HSQLDialect
- 13:44:16,834 INFO SettingsFactory:90 - Default batch fetch size: 1
- 13:44:16,834 INFO SettingsFactory:94 - Generate SQL with comments: disabled
- 13:44:16,834 INFO SettingsFactory:98 - Order SQL updates by primary key: disabled
- 13:44:16,834 INFO SettingsFactory:273 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
- 13:44:16,836 INFO ASTQueryTranslatorFactory:21 - Using ASTQueryTranslatorFactory
- 13:44:16,837 INFO SettingsFactory:106 - Query language substitutions: {}
- 13:44:16,839 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
- 13:44:16,839 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 1
- 13:44:16,839 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
- 13:44:16,843 INFO DriverManagerConnectionProvider:80 - using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:file:G:\java\tests\testDB\CompteDB.monCompte
- 13:44:16,843 INFO DriverManagerConnectionProvider:86 - connection properties: {user=sa, password=****}
- 13:44:17,077 INFO SettingsFactory:148 - JDBC batch size: 15
- 13:44:17,077 INFO SettingsFactory:151 - JDBC batch updates for versioned data: disabled
- 13:44:17,078 INFO SettingsFactory:156 - Scrollable result sets: enabled
- 13:44:17,078 INFO SettingsFactory:164 - JDBC3 getGeneratedKeys(): disabled
- 13:44:17,080 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
- 13:44:17,081 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
- 13:44:17,082 INFO SettingsFactory:176 - Automatic flush during beforeCompletion(): disabled
- 13:44:17,082 INFO SettingsFactory:179 - Automatic session close at end of transaction: disabled
- 13:44:17,082 INFO SettingsFactory:260 - Cache provider: org.hibernate.cache.EhCacheProvider
- 13:44:17,084 INFO SettingsFactory:187 - Second-level cache: enabled
- 13:44:17,084 INFO SettingsFactory:192 - Optimize cache for minimal puts: disabled
- 13:44:17,085 INFO SettingsFactory:199 - Structured second-level cache entries: enabled
- 13:44:17,085 INFO SettingsFactory:203 - Query cache: disabled
- 13:44:17,085 INFO SettingsFactory:210 - Echoing all SQL to stdout
- 13:44:17,085 INFO SettingsFactory:214 - Statistics: disabled
- 13:44:17,085 INFO SettingsFactory:218 - Deleted entity synthetic identifier rollback: disabled
- 13:44:17,086 INFO SettingsFactory:232 - Default entity-mode: pojo
- 13:44:17,212 INFO SessionFactoryImpl:140 - building session factory
- 13:44:17,217 WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/G:/java/librairies/libHibernate/ehcache-1.1.jar!/ehcache-failsafe.xml
- 13:44:17,439 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
- 13:44:17,442 INFO Dialect:89 - Using dialect: org.hibernate.dialect.HSQLDialect
- 13:44:17,442 INFO Configuration:844 - processing extends queue
- 13:44:17,442 INFO Configuration:848 - processing collection mappings
- 13:44:17,442 INFO Configuration:857 - processing association property references
- 13:44:17,442 INFO Configuration:884 - processing foreign key constraints
- 13:44:17,443 INFO Configuration:844 - processing extends queue
- 13:44:17,443 INFO Configuration:848 - processing collection mappings
- 13:44:17,443 INFO Configuration:857 - processing association property references
- 13:44:17,443 INFO Configuration:884 - processing foreign key constraints
- 13:44:17,443 INFO SchemaExport:100 - Running hbm2ddl schema export
- 13:44:17,444 INFO SchemaExport:121 - exporting generated schema to database
- 13:44:17,444 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
- 13:44:17,444 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 1
- 13:44:17,445 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
- 13:44:17,446 INFO DriverManagerConnectionProvider:80 - using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:file:G:\java\tests\testDB\CompteDB.monCompte
- 13:44:17,446 INFO DriverManagerConnectionProvider:86 - connection properties: {user=sa, password=****}
- 13:44:17,449 DEBUG SchemaExport:136 - drop table COMPTE if exists
- 13:44:17,449 DEBUG SchemaExport:154 - create table COMPTE (
- ID_COMPTE integer not null,
- NOM_COMPTE varchar(255),
- NOM_BANQUE varchar(255),
- SOLDE_INITIAL double,
- primary key (ID_COMPTE)
- )
- 13:44:17,454 INFO SchemaExport:166 - schema export complete
- 13:44:17,455 INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:hsqldb:file:G:\java\tests\testDB\CompteDB.monCompte
- 13:44:17,455 INFO SessionFactoryImpl:366 - Checking 0 named queries
- 13:44:17,487 INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:hsqldb:file:G:\java\tests\testDB\CompteDB.monCompte
- Hibernate: insert into COMPTE (NOM_COMPTE, NOM_BANQUE, SOLDE_INITIAL, ID_COMPTE) values (?, ?, ?, ?)
- Hibernate: select compte0_.ID_COMPTE as ID1_, compte0_.NOM_COMPTE as NOM2_0_, compte0_.NOM_BANQUE as NOM3_0_, compte0_.SOLDE_INITIAL as SOLDE4_0_ from COMPTE compte0_
|
merci d'avance |