SequenceGenerators 名称在 hibernate 5 中是全局唯一的吗?
Are SequenceGenerators names globally unique in hibernate 5?
我正在从 Hibernate 4.3 升级到 5.2.13。我有一个模式,其中许多实体从抽象 class 继承属性,抽象 class 本身被注释。例如:
实体A:
@AttributeOverride(name = "id", column = @Column(name = "table_a_id"))
@Entity
@SequenceGenerator(name = "IdSequenceGenerator", sequenceName = "table_a_seq")
@Table(name = "table_a")
public class EntityA extends Identity {
}
实体 B:
@AttributeOverride(name = "id", column = @Column(name = "table_b_id"))
@Entity
@SequenceGenerator(name = "IdSequenceGenerator", sequenceName = "table_b_seq")
@Table(name = "table_b")
public class EntityB extends Identity {
}
父级 class:
@MappedSuperclass
public abstract class Identity {
private Long id;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "IdSequenceGenerator")
public Long getId()
{
return id;
}
}
这在 Hibernate 4 下工作正常,但我收到以下异常。通过这个,我假设序列生成器名称不需要是全局唯一的,但现在是。是这样吗?有没有一种方法可以通过配置而不是更改许多(许多)实体来解决这个问题?
我收到这个错误:
Caused by: java.lang.IllegalArgumentException: Duplicate generator name IdSequenceGenerator
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.addIdentifierGenerator(InFlightMetadataCollectorImpl.java:448)
at org.hibernate.cfg.AnnotationBinder.lambda$buildGenerators[=13=](AnnotationBinder.java:3369)
at java.util.HashMap.forEach(HashMap.java:1288)
at org.hibernate.cfg.AnnotationBinder.buildGenerators(AnnotationBinder.java:3368)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:728)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:249)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.processEntityHierarchies(MetadataBuildingProcess.java:222)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:454)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:439)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 54 more
感谢@maheshkumar。复制他的评论。
可以在实体 class 或主键字段或 属性 上指定序列生成器。生成器名称的范围对于持久化单元是全局的(跨所有生成器类型)。
我正在从 Hibernate 4.3 升级到 5.2.13。我有一个模式,其中许多实体从抽象 class 继承属性,抽象 class 本身被注释。例如:
实体A:
@AttributeOverride(name = "id", column = @Column(name = "table_a_id"))
@Entity
@SequenceGenerator(name = "IdSequenceGenerator", sequenceName = "table_a_seq")
@Table(name = "table_a")
public class EntityA extends Identity {
}
实体 B:
@AttributeOverride(name = "id", column = @Column(name = "table_b_id"))
@Entity
@SequenceGenerator(name = "IdSequenceGenerator", sequenceName = "table_b_seq")
@Table(name = "table_b")
public class EntityB extends Identity {
}
父级 class:
@MappedSuperclass
public abstract class Identity {
private Long id;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "IdSequenceGenerator")
public Long getId()
{
return id;
}
}
这在 Hibernate 4 下工作正常,但我收到以下异常。通过这个,我假设序列生成器名称不需要是全局唯一的,但现在是。是这样吗?有没有一种方法可以通过配置而不是更改许多(许多)实体来解决这个问题?
我收到这个错误:
Caused by: java.lang.IllegalArgumentException: Duplicate generator name IdSequenceGenerator
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.addIdentifierGenerator(InFlightMetadataCollectorImpl.java:448)
at org.hibernate.cfg.AnnotationBinder.lambda$buildGenerators[=13=](AnnotationBinder.java:3369)
at java.util.HashMap.forEach(HashMap.java:1288)
at org.hibernate.cfg.AnnotationBinder.buildGenerators(AnnotationBinder.java:3368)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:728)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:249)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.processEntityHierarchies(MetadataBuildingProcess.java:222)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:454)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:439)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 54 more
感谢@maheshkumar。复制他的评论。
可以在实体 class 或主键字段或 属性 上指定序列生成器。生成器名称的范围对于持久化单元是全局的(跨所有生成器类型)。