Remplacer hibernate.cfg.xml par un autre fichier XML c'est possible ?
Bonjour,
Je voudrais regrouper l'ensemble de fichiers de configuration de mon application dans un seul fichier XML. Je voudrais savoir si c'est possible de reprendre le code que j'ai dans hibernate.cfg.xml et le mettre dans mon nouveau fichier.
Si oui comment dois je procéder et faut il mettre mon nouveau fichier (le chemin).
Merci à vous
[Exception] configuration dynamique d'hibernate
Bonjour,
J'essaye de rendre les paramétres du user et le chemin de BD configurable à partir d'un autre fichier XML autre que hibernate.cfg.xml.
Pour cela j'utilise le code suivant :
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
| Configuration configuration = new Configuration();
configuration.configure();
/* Initialiser le fichier hibernate.cfg.xml */
Preferences dataBasePrefs = prefs.node("database");
Properties propreties1 = new Properties();
String str1 = dataBasePrefs.get("connection.username", "");
propreties1.put("connection.username", str1);
Properties propreties2 = new Properties();
String str2 = dataBasePrefs.get("connection.password", "");
propreties2.put("connection.password", str2);
Properties propreties3 = new Properties();
String str3 = dataBasePrefs.get("connection.url", "");
propreties3.put("connection.url", str3);
configuration.addProperties(propreties1);
configuration.addProperties(propreties2);
configuration.addProperties(propreties3);
SessionFactory sessionFactory = configuration.buildSessionFactory();
HibernateUtil.setSessionFactory(sessionFactory);
/* Fin initialisation */ |
Mais j'ai l'exception suivante :
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
| java.lang.UnsupportedOperationException: The user must supply a JDBC connection
at org.hibernate.connection.UserSuppliedConnectionProvider.getConnection(UserSuppliedConnectionProvider.java:30)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:423)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.beginTransaction(Unknown Source)
at com.ullink.extranet.module.orderBookMonitoring.dao.OrderBook.DaoOrderBookImpl.getAllTblconfeventsync(DaoOrderBookImpl.java:746)
at com.ullink.extranet.module.orderBookMonitoring.service.OrderBook.ServiceOrderBookImpl.getAllTblconfeventsync(ServiceOrderBookImpl.java:446)
at com.ullink.extranet.module.orderBookMonitoring.web.Connection.MemberAreaFacesServlet$1.run(MemberAreaFacesServlet.java:46)
at java.lang.Thread.run(Unknown Source)
L'exception est lancé quand on essaye de commencer une transaction (la 2eme ligne)
Code:
12 | Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction(); |
Voila aussi le fichier hibernate.cfg.xml que j'utilise :
<?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>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<!--<property name="connection.username">postgres</property>
<property name="connection.password">postgres</property>
<property name="connection.url">jdbc:postgresql://localhost/db_memberarea</property>-->
<!-- Echo all executed SQL to stdout
<property name="show_sql">true</property>-->
<property name="hibernate.transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- <property name="hibernate.jdbc.batch_size">0</property> -->
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<mapping resource="com/ullink/extranet/module/orderBookMonitoring/mapping/Tblorder.hbm.xml" />
<mapping resource="com/ullink/extranet/module/orderBookMonitoring/mapping/Tbltrade.hbm.xml" />
<mapping resource="com/ullink/extranet/module/orderBookMonitoring/mapping/Tblconfeventsync.hbm.xml" />
</session-factory>
</hibernate-configuration> |
Est ce que vous avez une idée concernant cette erreur ?
Merci