Hibernate没有找到map属性

时间:2017-07-31 21:05:22

标签: java hibernate

所以我得到了例外: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: se.mulander.cosmos.movies.model.Cast.starredIn in se.mulander.cosmos.movies.model.ExtendedMovie.cast

但我无法弄清楚原因。 我要映射的两个对象是:

@Entity
@Table(name = "cast")
@ApiModel(description = "A cast member that has been part of making the movie")
public class Cast
{
    @JsonIgnore
    @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
    @JoinColumn(name = "movie_id")
    public ExtendedMovie starredIn;
}

@Entity
@Table(name = "extended_movie")
public class ExtendedMovie
{
    @OneToMany(cascade = {CascadeType.ALL}, mappedBy = "starredIn", orphanRemoval = true)
    @LazyCollection(LazyCollectionOption.FALSE)
    public List<Cast> cast = new ArrayList<>();
}

我已经删除了其他一些属性,但实际上这是无效的关系。

所以我没有得到的是为什么它说它是一个未知的属性,因为属性是公共的,而hibernate应该没有映射它的任何问题。

我在这里失踪的是什么?

1 个答案:

答案 0 :(得分:1)

尝试类似:

ExtendedMovie

@Entity
public class ExtendedMovie implements Serializable {
    private static final long serialVersionUID = 6771189878622264738L;
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    @JoinColumn(name = "cast_id", referencedColumnName = "id")
    private Set<Cast> cast;

    public Set<Cast> getCast() {
        return cast;
    }

    public void setCast(Set<Cast> cast) {
        this.cast= cast;
    }
}

<强>角色:

@Entity
@ApiModel(description = "A cast member that has been part of making the movie")
public class Cast implements Serializable {
    private static final long serialVersionUID = 6771189878622265738L;
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    //Remove extendedmovie from here
    //other property getter and setters here
}

这将建立ExtendedMovie和Cast之间的一对多关系。