Duplicate generator sequence hibernate on subclasses

Bruno Carletti :

I follow this post to resolve my initial problem: Specifying distinct sequence per table in Hibernate on subclasses

But now I get an exception:

Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Duplicate generator name idgen

Below my class, subclass and pom.xml:

EntityId (abstract class)

@MappedSuperclass
public abstract class EntityId<T extends Serializable> implements Serializable {

    private static final long serialVersionUID = 1974679434867091670L;

    @Id
    @GeneratedValue(generator="idgen", strategy=GenerationType.SEQUENCE)
    @Column(name="id")
    protected T id;

    public T getId() {
        return id;
    }

    public void setId(T id) {
        this.id = id;
    }

}

Category class

@Entity
@SequenceGenerator(name="idgen", sequenceName="cat_id_seq", allocationSize=1)
@AttributeOverrides({
    @AttributeOverride(name="id", column = @Column(name="cat_id"))
})
@Table(name="categoria")
public class Category extends EntityId<Integer> {

    private static final long serialVersionUID = -870288485902136248L;

    @Column(name="name")
    private String name;

    @Column(name="description")
    private String description;

}

pom.xml

...
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.15.Final</version>
</dependency>
...

My problem it's similar with this post: https://hibernate.atlassian.net/browse/HHH-12329

uli :

From the link you provided. The JPA spec says that:

A sequence generator may be specified on the entity class or on the primary key field or property. The scope of the generator name is global to the persistence unit (across all generator types).

So, it's not legal to have two identifier generators with the same name and different configurations. The scope is global, not entity.

To resolve your issue you should push the @Id from the @MappedSuperclass into subclasses.

More details


Edited, added possible workaround:

  • remove annotation from field in super class;
  • make getter abstract;
  • let all sub-classes have their own sequence generator: all generators should have global unique name;
  • implement getter;
  • move annotations related to Id field on getter.

public interface EntityId<T extends Serializable> extends Serializable {

    public T getId();

    public void setId(T id);

}

@Entity
@Table(name="categoria")
public class Category implements EntityId<Integer> {

    private static final long serialVersionUID = -870288485902136248L;

    @Id
    @Column(name="cat_id")
    @SequenceGenerator(name="cat_id_gen", sequenceName="categoria_cat_id_seq", allocationSize=1)
    @GeneratedValue(generator="cat_id_gen", strategy=GenerationType.SEQUENCE)
    private Integer id;

    //others attributes here...

    @Override
    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public Integer getId() {
        return this.id;
    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related