Database creation not working in eclipselink with glassfish 4 and Postgres

别来无恙 提交于 2021-01-27 13:11:33

问题


I want to create the database based on Entities. Configuration:

  • Glassfish: GlassFish Server Open Source Edition 4.1 (build 13)
  • Eclipselink: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd (delivered with glassfish)
  • Database: PostgreSQL Version: 9.4.2
  • Driver: PostgreSQL Native Driver Version: PostgreSQL 9.4 JDBC4.1 (build 1201)

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:

  • SELECT ID FROM table_name WHERE ID <> ID
  • SELECT 1

This is repeated 4-5 times. The first query gives the following obvious postgres error:

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: ERROR: current transaction is aborted, commands ignored until end of transaction block

After this eclipselink continues creating the tables:

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.

Edit: 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>

回答1:


Have you tried setting the eclipselink.target-database property to PostgreSQL or org.eclipse.persistence.platform.database.PostgreSQLPlatform?

Please share your persistence.xml file if the above does not resolve the issue.




回答2:


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>

Obviously, once the tables are created, it is necessary to change the value of the property eclipselink.ddl-generation to none.

NOTE: The database was Derby, but I've also tried it with MySQL. I think that it will work with PostgreSQL.



来源:https://stackoverflow.com/questions/31319038/database-creation-not-working-in-eclipselink-with-glassfish-4-and-postgres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!