gpt4 book ai didi

java - SequenceGenerators 名称在 Hibernate 5 中全局唯一吗?

转载 作者:太空宇宙 更新时间:2023-11-04 10:33:33 25 4
gpt4 key购买 nike

我正在从 Hibernate 4.3 升级到 5.2.13。我有一个模式,其中许多实体从本身带有注释的抽象类继承属性。例如:

实体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 {

}

父类:

@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$0(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$1.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。复制他的评论。

序列生成器可以在实体类或主键字段或属性上指定。生成器名称的范围对于持久性单元是全局的(跨所有生成器类型)。

关于java - SequenceGenerators 名称在 Hibernate 5 中全局唯一吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49743023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com