How to avoid Openjpa set null value in autogenerated field?

86 views Asked by At

I am trying to migrate one application from websphere 8 to websphere 9. And the open jpa is updated from 2.0 to 2.1. After migrating I got error while saving. The autogenerated Id field is set to null and throws database exception.

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "TestID")
private Long testID

the error is given below

INSERT NTO TEST(Test_ID,Test_Value)
values(?,?) [params=(null) null DB2 SQL Error:SQLCODE=-798, SQLSTATE= 428C9
1

There are 1 answers

0
Cindy High On

This is a known difference between OpenJPA and EclipseLink that is covered by the WebSphere migration tools. The help for this migration issue includes:

"OpenJPA and EclipseLink create different tables to generate values for attributes that have either the @GeneratedValue or the @GeneratedValue(strategy=GenerationType.AUTO) annotation. If your application contains entities with attributes that were generated by OpenJPA, persisting new entities using EclipseLink causes an error because EclipseLink attempts to generate these values from the EclipseLink table.

This problem is resolved by configuring the entity to use the OpenJPA sequence table to generate the values of the attribute. By doing so, EclipseLink uses the last value listed in the OpenJPA table to generate the attribute value."

The migration tools can be downloaded from: https://ibm.biz/wamtdownloads. I encourage you to first use the binary scanner to quickly scan your application archive and see the full rule help and information on any other potential issues. In this case, there is a code quick fix in the Eclipse tools that will assist you in making the code changes to move to EclipseLink.

The migration tools can be used for moving between traditional WebSphere versions, from traditional WebSphere to Liberty, between Liberty Java EE levels, and moving between Java SE versions.

By the way, you can configure WebSphere v9 to use JPA 2.0 if you prefer: https://www.ibm.com/docs/en/was/9.0.5?topic=applications-identifying-modifying-jpa-specification-level