【问题标题】:hibernateProperties.cfg - access hibernate property from JavahibernateProperties.cfg - 从 Java 访问休眠属性
【发布时间】:2015-01-12 01:18:07
【问题描述】:

我的会话工厂是这样用 XML 描述的:

>    <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>

是否可以从我的 Java 代码中访问 CUSTON 属性,我尝试了以下方法:

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

或者来自 sessionFactory 对象但没有成功,无论如何我可以做到这一点?

【问题讨论】:

    标签: java hibernate configuration


    【解决方案1】:

    试试这个代码

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

    【讨论】:

      猜你喜欢
      • 2012-07-02
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多