将嵌套的mappedBy与Hibernate

时间:2016-09-15 15:55:06

标签: hibernate jpa classcastexception

我有以下课程(简化):

位置

代表地理区域

@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对象的位置值等于该东西所在的位置。

问题

当我在位置类中添加的东西字段时,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$1.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等于给定位置的所有东西,对吗?

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

1 个答案:

答案 0 :(得分:2)

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

  

必须在mappedBy元素中使用点(“。”)表示法语法   指示嵌入属性中的关系属性。该   与点表示法一起使用的每个标识符的值是   相应的嵌入式领域或财产。