爲什麼你需要將所有標籤複製到它們中的每一個,如果你將兩個持久性單元使用相同的實體?
,爲什麼你就是不使用
<exclude-unlisted-classes>false</exclude-unlisted-classes>
例如:
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="persistenceUnit_1">
<provider> xxx </provider>
<jta-data-source> xxxx </jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
//Properties of first persistence unit
</properties>
</persistence-unit>
<persistence-unit name="persistenceUnit_2">
<provider> xxx </provider>
<jta-data-source> xxxx </jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
//Properties of second persistence unit
</properties>
</persistence-unit>
</persistence>
在這種情況下,你需要通過名字來使用它,例如
@PersistenceContext(unitName = "persistenceUnit_1")
private EntityManager entityManager;
聽起來不錯。我不知道你可以傳遞第二個參數。我會嘗試。謝謝! – Wombat
@Wombat有沒有運氣? – coladict
您的解決方案正在運行,我正在使用它作爲當前的解決方案。但它有一些薄弱點:傳入「hibernate.hbm2ddl.auto」不起作用(因爲它不是JPA標準參數)。如果我們將這個參數保存在「單個」persistence.xml文件中,那麼我們有可能會忘記將其關閉,並且生產數據庫將被清除。所以我寧願有多個持久性。xml但不起作用,因爲我必須在那裏列出所有實體,否則它們將不會被EntityManagerFactory找到。 – Wombat