'MappedSuperclass - moving SequenceGenerator id up from all child classes to parent class

I was following MappedSuperclass - Change SequenceGenerator in Subclass to move id field up to the AbstractEntity from all the child entity classes but also keeping their distinct sequences.

All entities extend a superclass called AbstractEntity like this:

@MappedSuperclass
@EntityListeners(RecordAuditorListener.class)
public class AbstractEntity implements Serializable {

    @Id
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID_GEN")
    protected Integer id;
...

The child entity named Product:

@Entity
@SequenceGenerator(name = "ID_GEN", sequenceName = "SQ_PRODUCT", allocationSize = 1)
@Table(name = "PRODUCT")
@EntityListeners({OnProductChangeSyncRequestCreatorListener.class})
@XmlRootElement
public class Product extends AbstractEntity implements Attributable {
 
 //This id field was removed from here
 //  @Id
 //  @Column(name = "ID", nullable = false)
 //  @GeneratedValue(generator = "SQ_PRODUCT")
 //  @SequenceGenerator(name = "SQ_PRODUCT", sequenceName = "SQ_PRODUCT", allocationSize = 1)
 //  private Integer id;
...

The child entity named Partner:

@Entity
@SequenceGenerator(name = "ID_GEN", sequenceName = "SQ_PARTNER", allocationSize = 1)
@Table(name = "PARTNER")
@EntityListeners({OnProductChangeSyncRequestCreatorListener.class})
@XmlRootElement
public class Partner extends AbstractEntity implements Attributable {
 
 //This id field was removed from here
 //  @Id
 //  @Column(name = "ID", nullable = false)
 //  @GeneratedValue(generator = "SQ_PARTNER")
 //  @SequenceGenerator(name = "SQ_PARTNER", sequenceName = "SQ_PARTNER", allocationSize = 1)
 //  private Integer id;
...

When running the application I get this error message.

Exception Description: Conflicting annotations with the same name [ID_GENERATOR] were found. The first one [@javax.persistence.SequenceGenerator({allocationSize=1, name=ID_GENERATOR, sequenceName=sq_PARTNER})] was found within [...] and the second [@javax.persistence.SequenceGenerator({allocationSize=1, name=ID_GENERATOR, sequenceName=sq_PR_PRODUCT})] was found within [...]. Named annotations must be unique across the persistence unit..

In the related question it says that this is an incorrect solution in eclipselink 2.6.2. I am using eclipselink 2.6.9. is there a way that it can be done?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source