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

问题描述

我按照这篇文章来解决我的第一个问题:为子类在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:

在我的类,子类和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:

因此,具有相同名称和不同配置的两个标识符生成器是不合法的.范围是全局的,不是实体.

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

}

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

10-28 05:51