通过使用 CDI,如下面的代码所示:
@PersistenceUnit
EntityManagerFactory emf;
我想注入我的休眠EntityManagerFactory
目前,如果我执行下一行:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("HibernatePersistanceProv");
它按预期工作,但如果我使用第一种方法来尝试使用 Derby 连接,我知道这一点,因为我收到下一条错误消息:
org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
通过堆栈跟踪,我知道它是由于这个原因引起的。
Error connecting to server localhost on port 1527 with message Connection refused.
我知道这是因为它正在尝试连接到(Java DB)Derby db。
我的 persistence.xml 看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<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="HibernatePersistanceProv" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/aschema"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/aschema"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
</persistence>
我正在阅读,显然我需要指定 astandalone.xml
来提供不同的<jta-data-source>
,但它对我来说比它应该的要复杂一些(我不想追逐错误的兔子),我来自 Java EE 世界而所以我认为我的自我是全新的(对于傻瓜解释受到广泛赞赏)。
(如果有帮助)我在 GlassFish 4.1 服务器上运行。请询问是否需要任何其他信息。