0

私は休止状態とダービーを使用しています。

私は hibernate.cfg.xml を持っており、db waas を操作して Session を取得するために行ったことはすべて:

  return new AnnotationConfiguration().configure( "files/hibernate.cfg.xml"   ).buildSessionFactory().getCurrentSession();

私のhibernate.cfg.xmlには含まれています

   <property name="connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
   <property name="connection.url">jdbc:derby:crmdb;create=true</property>

およびエンティティ クラスのその他のプロパティとマッピング。

実行時にderby dbとbootPasswordのdataEncryptionを設定したいと思います。

hibernate.cfg.xml を変更しました:

    <property name="connection.url">jdbc:derby:crmdb;create=true;dataEncryption=true;bootPassword=myPass</property>

そしてすべてが大丈夫でした。

ここで、実行時に bootPassword を設定したいと思います。exby 環境変数から読み取ります。それが問題です!「connection.url」を hibernate.cfg.xml から削除してコード内に設定しようとすると、次のエラーが発生します。

 java.lang.UnsupportedOperationException: The application must supply JDBC connections

また、bootPassword だけを削除すると、db に接続できなくなります。

何か案が ?

4

2 に答える 2

0

sessionFactory を構築する前に、構成を変更する必要があります。

private SessionFactory sessionFactory;

public SessionFactory getSessionFactory(){
    if(sessionFactory==null){
        Configuration configuration = new AnnotationConfiguration().configure( "files/hibernate.cfg.xml"   );
        configuration.setProperty("hibernate.connection.url", "jdbc:derby:crmdb;create=true;dataEncryption=true;bootPassword="+password);
        configuration.configure();
        sessionFactory = configuration.buildSessionFactory();
    }
    return sessionFactory;
}

その他の注意事項: 毎回新しい SessionFactory を再作成することは避ける必要があります (時間がかかり、多くのリソースを消費し、役に立たない)。つまり、bootPassword ごとに 1 つの sessionFactory を作成する必要があります (これは唯一の動的部分であるため)。bootPassword が 1 つしかない場合 (つまり、単一の DB がある場合)、sessionFactory はシングルトンにすることができます/する必要があります。

参照

于 2013-11-15T10:25:44.363 に答える