初始化entityManagerFactory时发生MappingException

时间:2019-01-16 08:16:32

标签: hibernate spring-boot jpa

我正在设置一个初始化数据库上下文的Spring Boot(2.1.2-RELEASE)应用程序。数据库上下文已经在其他spring应用程序(3.2.18-RELEASE)中配置并且可以正常运行。 启动应用程序时,出现以下错误消息:

org.hibernate.MappingException: property [identifiers] not found on entity [my.package.collection.CollectionIdentifier]

通过提供属性,数据库的初始化是Spring Boot的标准 spring.datasource.*spring.jpa.*

CollectionIdentifier实体中不存在“ identifiers”属性下面的代码中可见的内容。但是,它存在于CollectionMaster实体中。这就是应该的方式

包含“标识符”属性的CollectionMaster实体

@Entity
@Audited
public class CollectionMaster extends AbstractPersistentObject implements Identifiable<CollectionIdentifier> {

    private static final String COLLECTION_ID = "collection_id";

    @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL, orphanRemoval=true)
    @JoinColumn(name=COLLECTION_ID)
    @Valid
    private Set<CollectionIdentifier> identifiers;  

    public final Set<CollectionIdentifier> getIdentifiers() {
        if(identifiers == null) {
            identifiers = new HashSet<>();
        }
        return identifiers;
    }

可识别的界面

public interface Identifiable<R extends AbstractIdentifier> {

    Set<R> getIdentifiers();

CollectionIdentifier实体

@Entity
@Audited
public class CollectionIdentifier extends AbstractIdentifier implements Comparable<CollectionIdentifier> {

    @Column(length=SIZE_3)
    private String type;

    @Column(length=SIZE_3)
    private String identifierType;

    @NotNull
    @Column(nullable=false, length=SIZE_65)    
    private String value;

AbstractIdentifier映射的超类

@MappedSuperclass
public abstract class AbstractIdentifier extends AbstractPersistentObject {

    @Column(length=SIZE_50)
    private String typeName;

AbstractPersistentObject映射的超类

@MappedSuperclass
public abstract class AbstractPersistentObject implements PersistentObject, Serializable {

    @Id
    @Column(length = SIZE_32)
    private String id = IdGenerator.createId();

    @Version
    private Integer version;

错误的堆栈跟踪:

Caused by: org.hibernate.MappingException: property [identifiers] not found on entity [my.package.collection.CollectionIdentifier]
at org.hibernate.mapping.PersistentClass.getProperty(PersistentClass.java:514) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.mapping.PersistentClass.getProperty(PersistentClass.java:525) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.cfg.IndexOrUniqueKeySecondPass.doSecondPass(IndexOrUniqueKeySecondPass.java:83) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1696) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1664) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:287) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:904) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:935) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) ~[spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) ~[spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) ~[spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
... 16 common frames omitted

现在的问题是,为什么上下文在初始化级别要求该属性在CollectionIdentifier实体中可用。

编辑

几个实体会发生错误,但仅当层次结构级别大于1时才会出现

例如:

CollectionMaster
    @OneToMany
    Set<CollectionIdentifier>

可以,但是

Collection
    @ManyToOne
    Set<CollectionMaster>
        @OneToMany
        Set<CollectionIdentifer>

引发错误

1 个答案:

答案 0 :(得分:1)

回答我自己的问题:

在休眠内核中进行了大量调试后,我在一个我认为完全不相关的地方找到了问题。

数据库实体是通过maven从其他模块导入的。该模型是使用休眠4.x设计的。该错误的原因是,导致该错误的所有实体上均已弃用@org.hibernate.annotations.Index。删除注释可解决此问题-尽管我认为这是一个休眠错误。