我使用Java和hibernate 4.3。@PersistenceUnit EntityManagerFactory使用不正確的數據庫(sun-appserv-samples)
如果我手動創建一個工廠:
entityManagerFactory = Persistence.createEntityManagerFactory("Chat");
一切正常。 JPA使用persistence.xml中指定的數據庫。
但是,如果我通過注射獲得的EntityManagerFactory:
@PersistenceUnit(unitName = "Chat")
private EntityManagerFactory factory;
JPA創建和使用太陽的appserv-samples數據庫。爲什麼?
順便提一句,在EntityManagerFactory.getProperties()中,鍵javax.persistence.jdbc.url ==我的正確數據庫。但這種行爲被忽略了。
演示項目:https://dl.dropboxusercontent.com/u/55489242/JPATest.zip
的persistence.xml:
<persistence version="2.1" 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">
<persistence-unit name="Chat" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/chat_db"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
<property name="hibernate.c3p0.acquire_increment" value="1" />
<property name="hibernate.c3p0.idle_test_period" value="60" />
<property name="hibernate.c3p0.min_size" value="1" />
<property name="hibernate.c3p0.max_size" value="2" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.timeout" value="0" />
<property name="hibernate.c3p0.acquireRetryAttempts" value="1" />
<property name="hibernate.c3p0.acquireRetryDelay" value="250" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.use_sql_comments" value="true" />
<property name="hibernate.current_session_context_class" value="thread" />
</properties>
</persistence-unit>
</persistence>
你是通過Spring或其他東西注入entityManager嗎?如果是的話你可以發佈代碼片段嗎? –
我使用JavaEE +休眠。我重寫了帖子並添加了演示項目。 – wishmaster35