@TableGenerator不使用EclipseLink JPA 2.0

时间:2012-11-04 20:40:54

标签: java mysql jpa eclipselink

我有一个奇怪的问题。 我在我的JPA应用程序中使用表生成策略。直到最近,我使用Toplink持久性提供程序作为JPA实现。一切正常。 刚才我添加了TopLink(Table per Class Inheritance)不支持的一些继承功能,所以我不得不转移到EclipseLink。 除ID生成外,一切正常。 以下是我用于所有唯一ID的代码示例:

@Id
@TableGenerator(name="INCOME_SEQ", table="SEQUENCE_TABLE", pkColumnName="SEQUENCE_NAME",
    valueColumnName="SEQUENCE_COUNT", pkColumnValue="INCOME_SEQ", allocationSize=1)
@GeneratedValue(strategy=GenerationType.TABLE, generator="INCOME_SEQ")
@Column(name = "INCOME_ID")
private Integer incomeId;

完全相同的代码适用于Toplink,但不适用于EclipseLink。 我得到的错误是:

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'INCOME_ID' cannot be null
Error Code: 1048

似乎根本没有使用Table生成......

这是我的MySql数据库中的表描述: 表名:SEQUENCE_TABLE

+----------------+--------------+------+-----+---------+-------+
| Field          | Type         | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| SEQUENCE_NAME  | varchar(64)  | NO   | PRI | NULL    |       |
| SEQUENCE_COUNT | int(11)      | NO   |     | 0       |       |
| localized_name | varchar(128) | YES  |     | NULL    |       |
| USER_ID        | int(11)      | NO   | MUL | 1       |       |
+----------------+--------------+------+-----+---------+-------+

任何人都可以指导我解决问题吗?或者至少是一种获取ID未创建的实际原因的方法?

编辑:这是Income类和表结构:

    +-------------------------+-------------------------------------------------------------------------------+------+-----+---------+-------+
| Field                   | Type                                                                          | Null | Key | Default | Extra |
+-------------------------+-------------------------------------------------------------------------------+------+-----+---------+-------+
| INCOME_ID               | int(11)                                                                       | NO   | PRI | NULL    |       |
| REFERENCE_DOCUMENT_ID   | varchar(32)                                                                   | YES  |     | NULL    |       |
| CLIENT_ID               | int(9)                                                                        | NO   | MUL | NULL    |       |
| income_date             | datetime                                                                      | NO   |     | NULL    |       |
| MOVEMENT_CATEGORY_ID    | int(11)                                                                       | NO   | MUL | NULL    |       |
| SUM_AFTER_TAX           | decimal(10,2)                                                                 | YES  |     | 0.00    |       |
| TOTAL_SUM               | decimal(10,2)                                                                 | NO   |     | NULL    |       |
| TAX_SUM                 | decimal(10,2)                                                                 | NO   |     | 0.00    |       |
| VAT_PERCENT             | decimal(10,2)                                                                 | NO   |     | NULL    |       |
| DESCRIPTION             | varchar(100)                                                                  | YES  |     | NULL    |       |
| STATUS                  | smallint(5) unsigned                                                          | YES  |     | 0       |       |
| REFERENCE_DOCUMENT_TYPE | enum('MANUAL_INVOICE','MANUAL_RECEIPT','INVOICE','RECEIPT','INVOICE_RECEIPT') | YES  |     | NULL    |       |
| USER_ID                 | int(11)                                                                       | NO   | MUL | 1       |       |
| UNIQUE_USER_SEQUENCE    | int(10) unsigned                                                              | YES  |     | NULL    |       |
+-------------------------+-------------------------------------------------------------------------------+------+-----+---------+-------+

编辑:我运行了应用程序,EclipseLink日志记录级别设置为最好,但我仍然看不到任何错误。我甚至可以看到序列正确初始化:

  [EL Finest]: 2012-11-07 20:37:00.359--ServerSession(1832413009)--Connection(1484802679)--Thread(Thread[pool-1-thread-2,5,main])--Connection released to connection pool [default].
[EL Finest]: 2012-11-07 20:37:00.405--ServerSession(1832413009)--Thread(Thread[pool-1-thread-2,5,main])--sequencing connected, state is NoPreallocation_State
[EL Finest]: 2012-11-07 20:37:00.406--ServerSession(1832413009)--Thread(Thread[pool-1-thread-2,5,main])--sequence SEQ_GEN_IDENTITY: preallocation size 1
[EL Finest]: 2012-11-07 20:37:00.406--ServerSession(1832413009)--Thread(Thread[pool-1-thread-2,5,main])--sequencing connected, state is Preallocation_Transaction_NoAccessor_State
[EL Finest]: 2012-11-07 20:37:00.406--ServerSession(1832413009)--Thread(Thread[pool-1-thread-2,5,main])--sequence RECEIPTS_SEQ: preallocation size 1
[EL Finest]: 2012-11-07 20:37:00.406--ServerSession(1832413009)--Thread(Thread[pool-1-thread-2,5,main])--sequence INCOME_SEQ: preallocation size 1

    [EL Finest]: 2012-11-07 20:38:09.264--UnitOfWork(514772947)--Thread(Thread[AWT-EventQueue-1,6,main])--persist() operation called on:  555 וובה.
[EL Finer]: 2012-11-07 20:38:09.265--UnitOfWork(514772947)--Thread(Thread[AWT-EventQueue-1,6,main])--begin unit of work commit
[EL Finest]: 2012-11-07 20:38:09.288--UnitOfWork(514772947)--Thread(Thread[AWT-EventQueue-1,6,main])--Execute query DoesExistQuery(referenceClass=MovementCategories )
[EL Finest]: 2012-11-07 20:38:09.29--UnitOfWork(514772947)--Thread(Thread[AWT-EventQueue-1,6,main])--Execute query InsertObjectQuery(חש' ידנית 555 וובה)
[EL Finest]: 2012-11-07 20:38:09.291--ServerSession(1832413009)--Connection(1484802679)--Thread(Thread[AWT-EventQueue-1,6,main])--Connection acquired from connection pool [default].
[EL Finer]: 2012-11-07 20:38:09.291--ClientSession(2117615354)--Connection(1484802679)--Thread(Thread[AWT-EventQueue-1,6,main])--begin transaction
[EL Fine]: 2012-11-07 20:38:09.293--ClientSession(2117615354)--Connection(1484802679)--Thread(Thread[AWT-EventQueue-1,6,main])--INSERT INTO income (INCOME_ID, DESCRIPTION, INCOME_DATE, REFERENCE_DOCUMENT_ID, REFERENCE_DOCUMENT_TYPE, STATUS, SUM_AFTER_TAX, TAX_SUM, TOTAL_SUM, UNIQUE_USER_SEQUENCE, VAT_PERCENT, CLIENT_ID, MOVEMENT_CATEGORY_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    bind => [null, null, 2012-11-07 20:37:55.225, 555, MANUAL_INVOICE, 1, 100.0, 16.5, 116.5, null, 16.5, 334, 3]

我看到的唯一线索是Preallocation_Transaction_NoAccessor_State属性...... 也许有人可以帮我理解这一点。在排序主键ID的情况下,记录输出应该是什么?

2 个答案:

答案 0 :(得分:0)

检查日志中的错误以及SQL失败的原因。

在您的表格def中,您还有其他几列?

USER_ID不是你告诉JPA的东西,并且不是null,所以我认为可能会导致失败。

如果仍有问题,请提供更多详细信息。

答案 1 :(得分:0)

找到解决方案,希望它对更多人有用: 出于某种原因,我为持久性单元设置了以下持久性配置设置:

persistenceMap.put(PersistenceUnitProperties.ID_VALIDATION,"NONE");

显然,关闭EclipseLink中的ID验证也会关闭ID生成。

感谢所有试图提供帮助的人!