Hibernate @PersistenceUnit EntityManagerFactory使用了不正确的数据库(sun appserv示例)

Hibernate @PersistenceUnit EntityManagerFactory使用了不正确的数据库(sun appserv示例),hibernate,jakarta-ee,jpa,Hibernate,Jakarta Ee,Jpa,我使用Java和Hibernate4.3 如果我手动创建工厂: entityManagerFactory = Persistence.createEntityManagerFactory("Chat"); 一切都好。JPA使用persistence.xml中指定的数据库 但如果我通过注射获得EntityManagerFactory: @PersistenceUnit(unitName = "Chat") private EntityManagerFactory factory; JPA创建并

我使用Java和Hibernate4.3

如果我手动创建工厂:

entityManagerFactory = Persistence.createEntityManagerFactory("Chat");
一切都好。JPA使用persistence.xml中指定的数据库

但如果我通过注射获得EntityManagerFactory:

@PersistenceUnit(unitName = "Chat")
private EntityManagerFactory factory;
JPA创建并使用sun appserv示例数据库。为什么?

顺便说一句,在EntityManagerFactory.getProperties()中,key javax.persistence.jdbc.url==我的正确数据库。但是这个特性被忽略了

演示项目:

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>

org.hibernate.jpa.HibernatePersistenceProvider
假的
在您提供的bundle(JPATest.zip)中,您可以将DAO作为测试(MyTest.java)或应用程序代码(作为web应用程序)执行。我认为在第二种情况下,您的应用程序找不到persistence.xml,因此它使用默认的NetBeans db。为了解决这个问题,请确保persistence.xml位于META-INF中,后者位于类路径中


顺便说一下,我建议您使用maven来构建项目及其标准项目文件夹布局(层次结构)。在本例中,您可以将persistence.xml放在路径src/main/resources/persistence.xml中。

您是通过Spring还是其他方式注入entityManager?如果是这样,你能发布代码片段吗?我使用JavaEE+hibernate。我重写了帖子并添加了演示项目。我写道:顺便说一句,在EntityManagerFactory.getProperties()中,key javax.persistence.jdbc.url==我正确的数据库。已加载persistence.xml。javax.persistence.jdbc.url是正确的。但JPA使用其他数据库。