4
私は、永続性のためにJPAを使用するJ2SEアプリケーション(エンタープライズコンテナなし)を作成しています。ここに私のpersistence.xml
です:J2SEアプリケーションのpersistence.xmlから資格情報を外部化
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="dbstats">
<!-- TODO: why is this needed? -->
<class>dreambear.stats.data.GamePlayedEvent</class>
<class>dreambear.stats.data.HyveMemberCountPoll</class>
<class>dreambear.stats.data.ProfileCountPoll</class>
<class>dreambear.stats.data.UserSession</class>
<class>dreambear.stats.data.UsersOnlinePoll</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<!-- TODO: externalize information -->
<property name="javax.persistence.jdbc.user" value="dbstats" />
<property name="javax.persistence.jdbc.password" value="*****" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://example.com/dbstats" />
</properties>
</persistence-unit>
</persistence>
これは、アプリケーションの中に「コンパイル」静的ファイル、です。しかし、実行時に設定パラメータからロードできるように、資格情報を抽出する必要があります。アプリの開発版とライブ版。
私はデフォルトの方法で持続性の設定をロードします。
emf = Persistence.createEntityManagerFactory("dbstats");
私は、このセットアップで資格情報を外部化するにはどうすればよいですか?私は実行時にpersistence.xmlファイルを生成することができましたが、それは少しハッキリです。