2009-08-27 116 views
14

我有hibernate.cfg.xml文件。運行時休眠配置

<session-factory> 

    <!-- Database connection settings --> 
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="connection.url"></property> 
    <property name="connection.username"></property> 
    <property name="connection.password"></property> 

    <!-- JDBC connection pool (use the built-in) --> 
    <property name="connection.pool_size">1</property> 

.....................

這是最有趣的部分文件。 現在我必須設置缺失值:網址,用戶名,密碼。 我想以這樣的方式來做到:

public static void SetSessionFactory() { 
    try { 

     AnnotationConfiguration conf = new AnnotationConfiguration().configure(); 
     // <!-- Database connection settings --> 
     conf.setProperty("connection.url", URL); 
     conf.setProperty("connection.username", USERNAME); 
     conf.setProperty("connection.password", PASSWORD); 
     SESSION_FACTORY = conf.buildSessionFactory(); 

    } catch (Throwable ex) { 
     // Log exception! 
     throw new ExceptionInInitializerError(ex); 
    } 
    } 

,但它只是加載從hibernate.cfg.xm我的配置,不改變任何財產......

URL,用戶名,passoword - 被命令行參數,所以我必須在運行時設置它們。

+0

哪個命名空間應該用於在asp.net MVC中使用「AnnotationConfiguration」? – bluwater2001 2010-03-26 18:27:42

回答

22

試着在這裏打電話conf.configure();
和屬性可能需要有休眠前綴,如「hibernate.connection.username」
希望它有幫助。

+0

maaaaaagic^_^ 添加了hibernate。到cfg文件和SetSessionFactory方法中的屬性名稱,現在所有的作品! – Oleksandr 2009-08-27 15:48:36

1

Environment

3

使用常量嘗試喜歡這個工作正常

AnnotationConfiguration的conf =新

AnnotationConfiguration()配置( 「/ dronehibernate.cfg.xml」)。

conf.setProperty("hibernate.connection.url","jdbc:mysql://localhost/PAT_DRONE_DB1"); 

    SessionFactory sessionFactory = conf.buildSessionFactory(); 

    Session session = sessionFactory.openSession(); 

    List<NetworkType> channelList = session.createQuery("from NetworkType").list();