我正在使用带有Glassfish的EclipseLink 2.4.1和用于持久化实体的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>
额外信息
drop-and-create-tables确实有效
我使用merge(实体)来持久化新实体(因为它们有很多对一个字段会导致重复的主要id问题) - 我觉得这可能是个问题。
查看MySQL日志和EclipseLink中最精细的日志级别,EclipseLink首先尝试从数据库中选择实体,如下所示:
mysql_log:
121115 10:49:03 9 Query SELECT t0.LISTINGID, t0.DTYPE, ... , t0.TESTFIELD FROM the_entity...
这是最后一个mysql日志条目,意味着它在这里崩溃,它从不试图删除表等。这是否意味着你不能使用drop-and-create或create-or-extend进行合并?我只是做了谷歌,没有找到任何关于此的信息。
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();
答案 0 :(得分:3)
Glassfish拦截eclipselink.ddl-generation.output-mode属性并强制它写出文件,如下面的答案中所述:eclipselink does not generate tables from annotated JPA classes 因此,您需要检查是否已在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 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);