将嵌套的 mappedBy 与 Hibernate 一起使用时出现 ClassCastException

ClassCastException when using nested mappedBy with Hibernate

我有以下 classes(简体):

位置

表示一个地理区域

@Entity
@Table(name = "location")
public class Location extends DomainObject {

    @OneToMany(mappedBy = "lastPositionHistoric.location")
    private Set<Thing> things = new LinkedHashSet<>(0);

    public Location() {
    }

    public Set<Thing> getThings(){
        return things;
    }

    public void setThings(Set<Thing> things){
        this.things = things;
    }

}

东西

表示移动资产

@Entity
@Table(name = "thing")
public class Thing extends DomainObject {

    @OneToOne(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.REFRESH})
    @JoinColumn(name = "last_position_thing_historic_id")
    private ThingHistoric lastPositionHistoric;

    public Thing() {
    }

    public ThingHistoric getLastPositionHistoric() {
        return lastPositionHistoric;
    }

    public void setLastPositionHistoric(ThingHistoric lastPositionHistoric) {
        this.lastPositionHistoric = lastPositionHistoric;
    }

}

ThingHistoric

表示事物的 GPS 历史记录(我已针对该问题删除了不相关的 GPS 属性)

@Entity
@Table(name = "thing_historic")
public class ThingHistoric extends DomainObject {

    @ManyToOne(cascade = {CascadeType.REFRESH, CascadeType.MERGE}, fetch = FetchType.EAGER)
    @JoinColumn(name = "thing_id", nullable = false)
    @NotNull
    private Thing thing;

    @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH})
    @JoinColumn(name = "location_id")
    private Location location;

    public ThingHistoric() {

    }

    public Thing getThing() {
        return thing;
    }

    public ThingHistoric setThing(Thing thing) {
        this.thing = thing;
        return this;
    }

    public Location getLocation() {
        return location;
    }

    public ThingHistoric setLocation(Location location) {
        this.location = location;
        return this;
    }
}

说明

每个事物都在现实世界中移动,并且可以在给定时间在一个位置内或不在任何位置。

当物体在某个位置内时,lastPositionHistoric 对象的位置值等于物体所在的位置。

问题

当我在 Location class 中添加 things 字段时,Hibernate 开始抱怨以下错误:

Caused by: java.lang.ClassCastException: org.hibernate.mapping.ManyToOne cannot be cast to org.hibernate.mapping.Component
    at org.hibernate.mapping.PersistentClass.getRecursiveProperty(PersistentClass.java:465)
    at org.hibernate.mapping.PersistentClass.getRecursiveProperty(PersistentClass.java:421)
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:763)
    at org.hibernate.cfg.annotations.CollectionBinder.secondPass(CollectionBinder.java:724)
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1589)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:848)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:876)
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
    ... 52 more

我不明白为什么,因为映射对我来说似乎是正确的。 位置内的事物列表应该是 lastPositionHisotric.location 等于给定位置的所有事物,对吗?

这是一个错误还是我的映射肯定是错误的?

“。” mappedBy 中的(点)表示法是指嵌入属性中的字段(例如,参见 JPA 规范 11.1.4.1)。它不适用于实体!而且我没有看到您的映射中嵌入任何内容。所以是的,你的映射肯定是错误的

The dot (".") notation syntax must be used in the mappedBy element to indicate the relationship attribute within the embedded attribute. The value of each identifier used with the dot notation is the name of the respective embedded field or property.