我的实体结构如下:
@MappedSuperclass
public abstract class BaseEntity {
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqGenerator")
private Long id;
}
@MappedSuperclass
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@SequenceGenerator(name = "seqGenerator", sequenceName = "DICTIONARY_SEQ")
public abstract class Intermed extends BaseEntity {}
@Entity
public class MyEntity1 extends Intermed {}
@Entity
public class MyEntity2 extends Intermed {}
我得到以下异常:
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sessionFactory' defined in class path resource [context/applicationContext.xml]:
Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown Id.generator: seqGenerator
当我在 Intermed 类上将 @MappedSuperclass 更改为 @Entity 时,一切正常。 使用@MappedSuperclass 和@SequenceGenerator 有什么问题吗?或者我错过了什么?
最佳答案
我在尝试实现应用程序范围的 ID 生成器时遇到了这个问题中描述的相同问题。
解决方案其实在第一个答案中:将序列生成器放在主键字段上。
像这样:
@MappedSuperclass
public abstract class BaseEntity {
@Id
@SequenceGenerator(name = "seqGenerator", sequenceName = "DICTIONARY_SEQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqGenerator")
private Long id;
}
@MappedSuperclass
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Intermed extends BaseEntity {}
@Entity
public class MyEntity1 extends Intermed {}
@Entity
public class MyEntity2 extends Intermed {}
虽然以这种方式做事似乎非常愚蠢(至少对我而言),但它确实有效。
关于java - @SequenceGenerator 在用@MappedSuperclass 注释的类上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3607691/