重复的生成器序列在子类上休眠 [英] Duplicate generator sequence hibernate on subclasses

查看:193
本文介绍了重复的生成器序列在子类上休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照这篇文章来解决我的第一个问题: 为子类在Hibernate中为每个表指定不同的顺序

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

但是现在我得到一个例外:

But now I get an exception:

调用init方法失败;嵌套的例外是 java.lang.IllegalArgumentException:重复的生成器名称idgen

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

在我的类,子类和pom.xml下面:

Below my class, subclass and pom.xml:

EntityId(抽象类)

@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;
    }

}

类别

@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>
...

我的问题与该帖子类似: https://hibernate.atlassian.net/browse /HHH-12329

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

推荐答案

通过您提供的链接. JPA规范指出:

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.

要解决您的问题,您应该将@Id从@MappedSuperclass推送到子类中.

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

更多详细信息

已编辑,添加了可能的解决方法:

Edited, added possible workaround:

  • 从超类的字段中删除注释;
  • 使吸气剂抽象化;
  • 让所有子类都有自己的序列生成器:所有生成器应具有全局唯一名称;
  • 实现获取器;
  • 在getter上移动与ID字段相关的注释.
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;
    }

}

这篇关于重复的生成器序列在子类上休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆