2009-08-27 28 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> 

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

これはの最も興味深い部分ですファイル。 ここで欠落値を設定する必要があります:url、username、password。 私は、このようなやり方でやろうとしている:

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

? – bluwater2001

答えて

22

conf.configure();に電話してください。
プロパティは、 "hibernate.connection.username"のような接頭辞を持つ必要があります。
Environmentクラス

+0

maaaaaagic^_^ ハイバネートを追加しました。 cfgファイルとSetSessionFactoryメソッドのプロパティ名とすべての作品になりました! – Oleksandr

1

使用定数は、このそれが正常に動作している

AnnotationConfiguration confに=新しい

AnnotationConfiguration()を設定( "/ dronehibernate.cfg.xml")のように試してみてください。名前空間は、私はasp.net MVCで"AnnotationConfiguration"を使用するために使用すべき

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(); 
3

から

関連する問題