在 Hibernate 5 中使用 @Embeddable 构建 SessionFactory 时出现 NullPointerException

问题描述 投票:0回答:1

我正在从

xml
配置迁移到注释。 我有
Barrier.hbm.xml
文件,其中包含:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping schema="inv">
    <class name="Barrier" table="INV_BARRIER" >
        <composite-id access="field">
            <key-many-to-one name="coupon" class="domain.Coupon" column="COUPON_ID"/>
            <key-many-to-one name="underlyingUsage" class="domain.Underlying" column="UNDERLYING_USAGE_ID"/>
        </composite-id>
        <version name="version" type="int" insert="true" access="field">
            <column name="VERSION" default="0" />
        </version>
        <property name="barrierValue" type="big_decimal" column="BARRIER_VALUE"/>      
    </class>
</hibernate-mapping>

当我在课堂上没有任何注释的情况下启动应用程序时,我有一个例外:

org.hibernate.AnnotationException: No identifier specified for entity: domain.Barrier

所以我添加了注释:

package domain;

import legacy.HibernatePersistentObject;
import flexjson.JSON;
import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.*;
    @Entity
    @Table(name = "INV_BARRIER", schema = "inv")
    public class Barrier extends HibernatePersistentObject implements Serializable {
    
        @Embeddable
        public static class BarrierId implements Serializable {
    
            @ManyToOne
            @JoinColumn(name = "COUPON_ID")
            private Coupon coupon;
    
            @ManyToOne
            @JoinColumn(name = "UNDERLYING_USAGE_ID")
            private UnderlyingUsage underlyingUsage;
    
            //getters/setters/constructor/equals/hashcode
        }
    
        @EmbeddedId
        private BarrierId id;
    
        @Version
        @Column(name = "VERSION", nullable = false, columnDefinition = "int default 0")
        private Integer version;
        @Transient
        private Coupon coupon;
    
        private AbstractUnderlyingUsage underlyingUsage;
    
        @Column(name = "BARRIER_VALUE")
        private BigDecimal barrierValue;
      
    
        public Barrier() {
        }
    
    }

但现在情况更糟了,因为我刚刚得到:

    java.lang.NullPointerException
        at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processFkSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1730)
        at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1640)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:709)

没有更多信息。这是怎么回事?

java hibernate
1个回答
0
投票

这有点奇怪,但问题在于

Coupon
UnderlyingUsage
类。他们还扩展了其他类,我也必须向它们添加注释。一旦添加了
Coupon
UnderlyingUsage
的整个层次结构的注释,错误就消失了。

因此,如果有人面临同样的问题,请查看与您更改的课程相关的所有课程。

© www.soinside.com 2019 - 2024. All rights reserved.