@TableGenerator无法与EclipseLink JPA 2.0一起使用 [英] @TableGenerator not working with EclipseLink JPA 2.0

查看:76
本文介绍了@TableGenerator无法与EclipseLink JPA 2.0一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题。
我在JPA应用程序中使用表生成策略。直到最近,我还是使用Toplink持久性提供程序作为JPA实现。一切正常。
刚才我添加了TopLink不支持的某些继承功能(每个类继承的表),因此我不得不移至EclipseLink。
除ID Generation之外,其他所有方法都可以正常工作。
这是我用于所有唯一ID的代码示例:

I am having a weird issue. i am using Table Generation strategy in my JPA application. until recently, I used Toplink persistence provider as the JPA implementation. All worked fine. Just now I added some inheritance features that are not supported by TopLink (Table per Class Inheritance), so I had to move to EclipseLink. All works fine, except for the ID Generation. Here is the code example which I use for all my unique IDs:

@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。
我得到的错误是:

The exact same code works with Toplink but does not work with EclipseLink. The Error I get is:

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

似乎表代是根本不使用...

It seems like the Table generation is not used at all...

这是MySql数据库中的表说明:
表名:SEQUENCE_TABLE

Here is the Table Description in my MySql database: Table name: 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的实际原因的方法?

Can anyone please direct me to the problem?? or at least a way to get the actual reason the ID is not being created?

编辑:这是收入类和表结构:

This is the Income class and Table structure:

    +-------------------------+-------------------------------------------------------------------------------+------+-----+---------+-------+
| 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日志记录级别设置为最佳的情况下运行了该应用程序,但仍然看不到任何错误。我什至可以看到序列已正确初始化:

I ran the app with EclipseLink Logging level set to finest, but I still can't see anything wrong. I even can see the sequencing being correctly initialized:

  [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,则日志输出应该是什么?

The only clue I see is the Preallocation_Transaction_NoAccessor_State property... Maybe someone can help me understand this. What should be the logging output in case of sequenced primary key ID?

推荐答案

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

Found The Solution, hope it will be useful for some more people: For some reason I had the following persistence config setting set for my persistence unit:

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

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

Apparently turning off ID validation in EclipseLink also turns off ID Generation.

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

Thanks to all who tried to help!

这篇关于@TableGenerator无法与EclipseLink JPA 2.0一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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