使用Wildfly,EclipseLink和Postgres自动创建和/或扩展JPA表 [英] Automatically creating and/or extending JPA tables with Wildfly, EclipseLink and Postgres

查看:175
本文介绍了使用Wildfly,EclipseLink和Postgres自动创建和/或扩展JPA表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经在Wildfly 8.1.0上运行的Java EE应用程序作为服务器,Postgres作为我们的数据库系统与EclipseLink一起作为持久性提供程序(em>)(我之所以想研究成本从内置Hibernate迁移到EclipseLink并不重要)。



我已经习惯了在扩展相应实体类时通过新添加的列自动扩展现有数据库表的Hibernate功能,并且如果EclipseLink能够这样做 - 学习文档,EclipseLink提供了属性

 < property name =eclipselink.ddl-generationvalue =创建或 - 延伸桌 /> 

这对我来说听起来不错,但是一个简短的测试会导致幻灭的结果,它不起作用开箱即用(请参阅下面的最简单示例)。有趣的是,如果我只是在没有任何列的情况下在数据库中创建相应的表,表就会被填充并按我的意愿工作,但既不是 create-or-extend-tables 也不是 drop-and-create-tables 甚至不是 create-tables 工作 - 仅前两个,如果我添加提到的空的表。



有没有人遇到类似这样的情况并可能找到解决方案?



另外,关于事务的Postgres问题


如果发生错误,PostgreSQL似乎中止事务,因此在发生故障后访问数据库可能不可行直到调用回滚之后。 ( EclipseLink / FAQ / JPA / PostgreSQL


并且由于某些错误日志指向了交易问题,也许这可能是问题的一部分?



最小示例



persistence.xml
$ b

 <?xml version =1.0encoding =UTF-8?> 
< persistence xmlns =http://xmlns.jcp.org/xml/ns/persistenceversion =2.1>

< persistence-unit name =...transaction-type =JTA>
< provider> org.eclipse.persistence.jpa.PersistenceProvider< / provider>
< jta-data-source> java:jboss / datasources / PostgresDS< / jta-data-source>
< exclude-unlisted-classes> false< / exclude-unlisted-classes>
< shared-cache-mode> ENABLE_SELECTIVE< / shared-cache-mode>
<属性>
< property name =eclipselink.ddl-generationvalue =create-tables/>
< / properties>
< / persistence-unit>
< /余辉>

Name.java

  import javax.persistence.Entity; 
import javax.persistence.Id;

@Entity
公共类名称{

私人字符串ID;

私人字符串名称;

@Id
public String getId(){
return id;
}

public void setId(String id){
this.id = id;
}

public String getName(){
return name;
}

public void setName(String name){
this.name = name;



$ div $解析方案

你在日志中有很多例外,全部归因于使用JTA数据源并且是交易的一部分。有关详细信息,请参阅eclipse.org/forums/index.php/t/440610,但DDL在大多数事务中不起作用,因此您需要触发EclipseLink以预先加载持久性单元,其中包含eclipselink.deploy-on-startup =true持久性属性。


I have experienced a strange matter with a Java EE application running on Wildfly 8.1.0 as the server and Postgres as our database system in combination with EclipseLink as the persistence provider (the reasons why I want to study the costs of migrating from built-in Hibernate to EclipseLink don't matter here).

I was getting used to the Hibernate feature of automatically extending the existing database tables by newly added columns when extending the corresponding Entity classes and it would be charming if EclipseLink was able to do so, too - studying the documentation, EclipseLink provides the property

<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>

which sound good to me, but a short test lead to the disillusioning result that it does not work "out of the box" (see minimal example below). Interestingly, if I simply create the respective table in the database without any columns, the table gets filled and works as I wanted, but neither create-or-extend-tables nor drop-and-create-tables and even not create-tables works - the first two only, if I add the mentioned empty table.

Has anyone encountered a situation like this and possibly found a solution?

Additionally, I learned about a Postgres problem regarding transactions

PostgreSQL seems to abort the transaction if an error occurs, so accessing the database after a failure may not be possible until after calling rollback. (EclipseLink/FAQ/JPA/PostgreSQL)

and as certain error logs point to problems with transactions, maybe this could be a part of the problem?

Minimal Example

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">

    <persistence-unit name="..." transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>java:jboss/datasources/PostgresDS</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
        <properties>
            <property name="eclipselink.ddl-generation" value="create-tables"/>
        </properties>
    </persistence-unit>
</persistence>

Name.java

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Name {

    private String id;

    private String name;

    @Id
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

解决方案

You have numerous exceptions in the log, all due to using a JTA datasource and being apart of a transaction. See eclipse.org/forums/index.php/t/440610 for details, but DDL doesn't work inside most transactions, so you will need to trigger EclipseLink to load the persistence unit upfront with the "eclipselink.deploy-on-startup"="true" persistence property.

这篇关于使用Wildfly,EclipseLink和Postgres自动创建和/或扩展JPA表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆