EclipseLink“create-or-extend-tables”不工作:“...未知列...” [英] EclipseLink "create-or-extend-tables" not working: "...unknown column..."

查看:264
本文介绍了EclipseLink“create-or-extend-tables”不工作:“...未知列...”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将EclipseLink 2.4.1与Glassfish和MySQL数据库一起用于持久化实体。

我给一个实体添加了一个字段,当我试图坚持这个实体时,它说新字段是'unknown'。

我们如何使用新的'create - 或 - 扩展表'功能?我本以为它会为这个领域创造一个新的专栏。



一些相关信息如下,让我知道你是否想要更多。



预先感谢! p>

堆栈跟踪

  ... 
导致:异常[EclipseLink-4002](Eclipse持久性服务 - 2.4.1.v20121003 ad44345):org.eclipse.persistence.exceptions.DatabaseException
内部异常:com.mysql.jdbc.exceptions.jdbc4 .MySQLSyntaxErrorException:'字段列表'中的未知列't0.TESTFIELD'
错误代码:1054
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333)
...

persistence.xml

 < persistence-unit name =myApptransaction-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>
<属性>
< property name =eclipselink.ddl-generation.output-modevalue =database/>
< property name =eclipselink.jdbc.batch-writingvalue =Buffered/>
< property name =eclipselink.logging.levelvalue =INFO/>
< property name =eclipselink.ddl-generationvalue =create-or-extend-tables/>
< / properties>



/ strong>



drop-and-create-tables不起作用。

我使用merge(entity)来坚持新实体很多很多到一个领域,否则会导致重复的主要ID问题) - 我觉得这可能是问题。

查看MySQL日志以及EclipseLink中最好的日志级别,EclipseLink首先尝试从数据库中选择实体,如下所示:

mysql_log:

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

这是最后一个mysql日志条目,这意味着它在这里崩溃,它从不尝试删除表等。这是否意味着你不能使用合并与拖放和创建或创建或扩展?



EclipseLink日志记录:

  FINER:获得的客户端:64279491 
FINER:TX绑定到tx mgr,status = STATUS_ACTIVE
FINER:获取工作单元:161130796
FINEST:将克隆与参考nz.co.site.api.v1.ListedItemDetail@8d88ca1
FINEST:执行查询ReadObjectQuery(referenceClass = ListedItemDetail)
FINEST:从连接池获取的连接[read]。
FINEST:重新连接到外部连接池
FINE:SELECT t0.LISTINGID,t0.DTYPE,...,t0.TESTFIELD,...,从项目t0,LISTEDITEMDETAIL t1 WHERE((t0.LISTINGID AND((t1.LISTINGID = t0.LISTINGID)AND(t0.DTYPE =?)))
bind => [2参数绑定]
FINE:SELECT 1
FINEST:连接释放到连接池[read]。
警告:本地异常堆栈:
异常[EclipseLink-4002](Eclipse持久性服务 - 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-模式属性并强制它按照答案中的描述写入文件: eclipselink不会从带注释的JPA类生成表格
因此,您需要检查Glassfish中的java2db是否允许drop-and-create-tables工作。



create-or-extend-tables虽然需要数据库连接来查看数据库中的内容,因此当前不能用于sql-script输出模式。我不确定如何强制Glassfish不覆盖输出模式属性,但如果无法完成,则需要运行持久性单元端glassfish以使用此功能。



在glassfish中的解决方法是尝试类似这样的操作:

  Map properties = new HashMap(); 
properties.put(PersistenceUnitProperties.DDL_GENERATION,PersistenceUnitProperties.CREATE_OR_EXTEND);
// create-or-extend仅适用于数据库
properties.put(PersistenceUnitProperties.DDL_GENERATION_MODE,PersistenceUnitProperties.DDL_DATABASE_GENERATION);
//这会导致在refreshMetadata上发生DDL生成,而不是等到获得em为止
properties.put(PersistenceUnitProperties.DEPLOY_ON_STARTUP,true);
JpaHelper.getEntityManagerFactory(em).refreshMetadata(properties);


I am using EclipseLink 2.4.1 with Glassfish and a MySQL database for persisting entities.
I added a field to an entity, and when I try to persist this entity it says the new field is 'unknown'.
How are we supposed to use the new 'create-or-extend-tables' feature? I would have thought it would create a new column for this field.

Some relevant information is below, let me know if you want more.

Thanks in advance!

Stack Trace

...
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>

Extra Info

drop-and-create-tables does work.
I am using merge(entity) to persist new entities (as they have lots of many to one fields which causes duplicate primary id issues otherwise) - I feel this may be the issue.
Looking at the MySQL log, and the finest log level from EclipseLink, EclipseLink first tries to select the entity from the database as follows:
mysql_log:

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

This is the last mysql log entry, meaning that it crashes here, it never tries to drop the table etc. Does this mean you cannot use merge with drop-and-create or create-or-extend? I just did a google and didn't find any info on this.

EclipseLink Logging:

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

I use bean transaction management to merge new entities:

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

解决方案

Glassfish intercepts the eclipselink.ddl-generation.output-mode property and forces it write out to file as described in an answer here: eclipselink does not generate tables from annotated JPA classes So you will need to check that you've enabled java2db in Glassfish for "drop-and-create-tables" to work.

"create-or-extend-tables" though requires database connections to see what is in the database, and so does not currently work with the sql-script output mode. I am not sure how to force Glassfish to not overwrite the output-mode property, but if it cannot be done, you will need to run your persistence unit side glassfish to use this feature.

A workaround in glassfish would be to try something like this:

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);

这篇关于EclipseLink“create-or-extend-tables”不工作:“...未知列...”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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