EclipseLink "create-or-extend-tables"不工作:"...unknown column..."



我将EclipseLink 2.4.1与Glassfish和MySQL数据库一起用于持久化实体
我在一个实体中添加了一个字段,当我试图持久化这个实体时,它会说新字段是"未知的">
我们应该如何使用新的"创建或扩展表"功能?我本以为它会为这个领域创建一个新的专栏。

下面是一些相关信息,如果你想要更多,请告诉我。

提前感谢!

堆栈跟踪

...
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.4.1.v20121003 ad44345): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 't0.TESTFIELD' in 'field list'
Error Code: 1054
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333)
...

persistence.xml:

<persistence-unit name="myApp" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/mysql</jta-data-source>
<mapping-file>META-INF/orm.xml</mapping-file>
<properties>
<property name="eclipselink.ddl-generation.output-mode" value="database"/>
<property name="eclipselink.jdbc.batch-writing" value="Buffered"/>
<property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
</properties>

额外信息

删除并创建表确实有效
我使用merge(entity)来持久化新实体(因为它们有很多多对一字段,否则会导致重复的主id问题)-我觉得这可能是问题所在
查看MySQL日志和EclipseLink的最佳日志级别,EclipseLink首先尝试从数据库中选择实体,如下所示:
MySQL_log:

121115 10:49:03 9 Query SELECT t0.LISTINGID, t0.DTYPE, ... , t0.TESTFIELD FROM the_entity...

这是最后一个mysql日志条目,这意味着它在这里崩溃了,它从来没有试图删除表等。这是否意味着你不能使用merge和drop来创建、创建或扩展?我只是搜索了一下,没有找到任何关于这个的信息。

EclipseLink日志记录:

FINER: client acquired: 64279491
FINER: TX binding to tx mgr, status=STATUS_ACTIVE
FINER: acquire unit of work: 161130796
FINEST: Merge clone with references nz.co.site.api.v1.ListedItemDetail@8d88ca1
FINEST: Execute query ReadObjectQuery(referenceClass=ListedItemDetail )
FINEST: Connection acquired from connection pool [read].
FINEST: reconnecting to external connection pool
FINE: SELECT t0.LISTINGID, t0.DTYPE, ... , t0.TESTFIELD, ... , FROM ITEM t0, LISTEDITEMDETAIL t1 WHERE ((t0.LISTINGID = ?) AND ((t1.LISTINGID = t0.LISTINGID) AND (t0.DTYPE = ?)))
bind => [2 parameters bound]
FINE: SELECT 1
FINEST: Connection released to connection pool [read].
WARNING: Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.DatabaseException

我使用bean事务管理来合并新实体:

userTransaction.begin();
entityManager.merge(entity);
entityManager.flush();
userTransaction.commit();

Glassfish拦截eclipselink.ddl-generation.output-mode属性,并强制将其写入文件,如这里的答案所述:eclipselink不会从带注释的JPA类生成表因此,您需要检查是否在Glassfish中启用了java2db,以便"放置并创建表"正常工作。

不过,"创建或扩展表"需要数据库连接才能查看数据库中的内容,因此目前无法使用sql脚本输出模式。我不知道如何强制Glassfish不覆盖输出模式属性,但如果无法完成,则需要运行持久性单元侧的Glassfish才能使用此功能。

玻璃鱼的一个变通方法是尝试这样的方法:

Map properties = new HashMap();
properties.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.CREATE_OR_EXTEND);
//create-or-extend only works on the database
properties.put(PersistenceUnitProperties.DDL_GENERATION_MODE, PersistenceUnitProperties.DDL_DATABASE_GENERATION);
//this causes DDL generation to occur on refreshMetadata rather than wait until an em is obtained
properties.put(PersistenceUnitProperties.DEPLOY_ON_STARTUP, "true");
JpaHelper.getEntityManagerFactory(em).refreshMetadata(properties);

相关内容

最新更新