我正在查看一段時間之前創建的代碼,並注意到一些奇怪的東西。以編程方式包含實體類與持久性單元?
我以編程方式創建了一個持久性單元,由於需要用戶輸入數據庫的位置來讀取。
我的代碼如下
Map properties = new HashMap();
db = plan.db;
// Configure the internal EclipseLink connection pool
properties.put(TRANSACTION_TYPE, PersistenceUnitTransactionType.RESOURCE_LOCAL.name());
properties.put(JDBC_DRIVER, "net.ucanaccess.jdbc.UcanaccessDriver");
properties.put(JDBC_URL, "jdbc:ucanaccess://" + db + ";singleconnection=true;memory=true");
properties.put(JDBC_USER, "");
properties.put(JDBC_PASSWORD, "");
// properties.put("provider" , "org.eclipse.persistence.jpa.PersistenceProvider");
EntityManagerFactory emf2;
EntityManager em2;
emf2 = Persistence.createEntityManagerFactory("PU", properties);
em2 = emf2.createEntityManager();
有了這個,我能創造我的連接多次。
我注意到的問題是,我也有代碼,在我的「的persistence.xml」
<persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>db.Items</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value=""/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="javax.persistence.jdbc.driver" value="net.ucanaccess.jdbc.UcanaccessDriver"/>
<property name="javax.persistence.jdbc.password" value=""/>
</properties>
現在我注意到,我無法找到任何方式的「實體類」添加到這個「持久性單元」,但是我能夠正常運行我的代碼,就像這樣。
我很好奇,如果它只是覆蓋舊的特性和來自持續單位的同名?它仍然使用「db.Items」的持久類。
我只是想確保這是做到這一點的正確方法。
我正在修改我的代碼,所以當前無法運行它以查看是否刪除了我的PErsistence.xml中的所有內容會發生什麼,但我對此很好奇。
我也注意到「提供者」屬性被註釋掉了。我需要發佈嗎? (它包含在xml文件中)。
還有一個例子,我看到提到關於「服務器目標」被設置爲「否」或什麼的?對此有何評論?
感謝所有
謝謝,除了指定的屬性外,其他所有內容都保持不變? 只是想確保我沒有創造大量的持久性單位,這會吸引大聲笑。 我可能不會包含任何屬性(我應該把標籤放在那裏,還是沒有關係,你認爲)? 謝謝! – XaolingBao