使用Glassfish 4和Postgres在eclipselink中无法创建数据库 [英] Database creation not working in eclipselink with glassfish 4 and Postgres
问题描述
我想基于实体创建数据库.配置:
I want to create the database based on Entities. Configuration:
- Glassfish:GlassFish Server开源版4.1(内部版本13)
- Eclipselink:Eclipse Persistence Services-2.5.2.v20140319-9ad6abd(与Glassfish一起提供)
- 数据库:PostgreSQL版本:9.4.2
- 驱动程序:PostgreSQL本机驱动程序版本:PostgreSQL 9.4 JDBC4.1(内部版本1201)
从eclipselink开始创建数据库的那一刻起,在将许多日志参数置于最佳状态之后,我在日志中看到以下语句:
From the moment eclipselink starts creating the database I see the following statements in the logs after putting a lot of log parameters on finest:
- 从表名中选择ID,其中WHERE ID<> ID
- 选择1
重复4-5次.第一个查询给出以下明显的postgres错误:
This is repeated 4-5 times. The first query gives the following obvious postgres error:
org.postgresql.util.PSQLException:错误:关系"table_name"不存在位置:16
org.postgresql.util.PSQLException: ERROR: relation "table_name" does not exist Position: 16
结果,以下查询给出以下错误:
As a result a following queries give the following error:
org.postgresql.util.PSQLException:错误:当前事务已中止,命令被忽略,直到事务块结束
org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block
此eclipselink之后,继续创建表:
After this eclipselink continues creating the tables:
创建表table_name(ID BIGINT不为NULL,主键(ID))
CREATE TABLE table_name (ID BIGINT NOT NULL, PRIMARY KEY (ID))
但是会产生相同的错误.
But produces the same error.
由于某种原因,在事务中创建数据库.我发现源代码负责,但是我找不到交易如何开始.我可以提供更详细的信息,但也许现在有人已经可以提供帮助了.
For some reason creating the database happens in a transaction. I found the source code responsible, but I can't find out how the transaction is started. I can give more detailed information, but maybe somebody can already help now.
persistence.xml
The persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="h ttp://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="AntennasOperatingSystemServerPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<non-jta-data-source>MyDatabase</non-jta-data-source>
<class>... bunch of classes ... </class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/>
<property name="eclipselink.deploy-on-startup" value="true"/>
<property name="eclipselink.ddl-generation.output-mode" value="database"/>
<property name="eclipselink.logging.level.sql" value="FINEST"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.target-database" value="PostgreSQL" />
</properties>
</persistence-unit>
</persistence>
推荐答案
我前段时间做了类似的事情.以下配置可以正常工作.
I did something similar some time ago. The following configuration work well.
<persistence-unit name="EjemploJpaPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url"
value="jdbc:derby://localhost:1527/example"/>
<property name="javax.persistence.jdbc.driver"
value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="123"/>
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
很显然,一旦创建了表,就有必要将属性 eclipselink.ddl-generation
的值更改为 none
.
Obviously, once the tables are created, it is necessary to change the value of the property eclipselink.ddl-generation
to none
.
注意:数据库是Derby,但我也在MySQL上尝试过.我认为它将与PostgreSQL一起使用.
这篇关于使用Glassfish 4和Postgres在eclipselink中无法创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!