简体   繁体   中英

hibernateProperties.cfg - access hibernate property from Java

I have my session factory described in XML this way:

>    <session-factory>
>         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
>         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
>         <property name="hibernate.c3p0.min_size">3</property>
>         <property name="hibernate.c3p0.max_size">45</property>
>         <property name="hibernate.c3p0.timeout">300</property><!--seconds-->
>         <property name="hibernate.c3p0.idle_test_period">10</property><!-- seconds -->
>         <property name="hibernate.c3p0.acquire_increment">3</property>
>         <property name="hibernate.c3p0.autoCommitOnClose">true</property>
>         <property name="hibernate.c3p0.unreturnedConnectionTimeout">60</property>
>         <property name="hibernate.c3p0.debugUnreturnedConnectionStackTraces">true</property>
> 
>         
>         <property name="hibernate.connection.provider_class">
>             org.hibernate.connection.C3P0ConnectionProvider
>         </property>
>         
>         <!--<property name="connection.autoReconnect">true</property>-->
>         <!--<property name="connection.autoReconnectForPools">true</property>-->
>         <!--<property name="connection.is-connection-validation-required">true</property>-->
> 
>         <!-- Use EHCache but not the query cache. -->
>         <property name="cache.provider_class">
>             net.sf.ehcache.hibernate.SingletonEhCacheProvider
>         </property>
>         <property name="cache.use_query_cache">false</property>
>         <property name="cache.use_minimal_puts">false</property>
> 
>         <!-- Print SQL to stdout. -->
>         <property name="show_sql">false</property>
>         <property name="format_sql">true</property>
>         <property name="CUSTOM">Hello World</property>
>     </session-factory>

Would it be possible to access that CUSTON property from my Java code, I tried the following:

 Environment.getProperties().getProperty("CUSTOM")

Or either from the sessionFactory object but without success, anyway I can achieve this?

Try this code

Field f = SessionFactoryImpl.class.getDeclaredField("properties");
f.setAccessible(true);
Properties p = (Properties)f.get(sessionFactory);
Object custom = p.get("CUSTOM");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM