我有一个奇怪的问题。我在我的 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
似乎根本没有使用表生成......
这是我的 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 的实际原因的方法?
编辑:这是收入类和表结构:
+-------------------------+-------------------------------------------------------------------------------+------+-----+---------+-------+
| 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,日志输出应该是什么?