Attributes with automatically generated values require configuration

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.

This rule scans for attributes that are annotated with either @GeneratedValue or @GeneratedValue(strategy=GenerationType.AUTO). An automated will be provided where it will replace the flagged @GeneratedValue annotation with the following annotations:

  @TableGenerator(name = "OPENJPA_SEQUENCE_TABLE", table = "OPENJPA_SEQUENCE_TABLE", pkColumnName = "ID", valueColumnName = "SEQUENCE_VALUE", pkColumnValue = "0")
  @GeneratedValue(strategy = GenerationType.TABLE, generator = "OPENJPA_SEQUENCE_TABLE")

For example, the rule flags the @GeneratedValue annotation in the following field access entity class.

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class GeneratedValueEntity {

    @Id
    @GeneratedValue
    private int id;

}

The automated fix will modify the @GeneratedValue annotation and adds the @TableGenerator annotation.

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class GeneratedValueEntity {

    @Id
    @TableGenerator(name = "OPENJPA_SEQUENCE_TABLE", table = "OPENJPA_SEQUENCE_TABLE", pkColumnName = "ID", valueColumnName = "SEQUENCE_VALUE", pkColumnValue = "0")
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "OPENJPA_SEQUENCE_TABLE")
    private int id;
}

For information about this issue and other OpenJPA to EclipseLink migration issues, see the OpenJPA to EclipseLink JPA Migration: Mappings guide.