问题
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;
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...
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 | |
+----------------+--------------+------+-----+---------+-------+
Can anyone please direct me to the problem?? or at least a way to get the actual reason the ID is not being created?
Edit: 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 | |
+-------------------------+-------------------------------------------------------------------------------+------+-----+---------+-------+
Edit: 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]
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?
回答1:
Check the log for errors and what SQL is failing.
In your table def, you have a couple other columns?
USER_ID is not something you told JPA about, and is not-null, so I would assume could cause a failure.
If still having issues, include a lot more details.
回答2:
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");
Apparently turning off ID validation in EclipseLink also turns off ID Generation.
Thanks to all who tried to help!
来源:https://stackoverflow.com/questions/13222677/tablegenerator-not-working-with-eclipselink-jpa-2-0