JPA提供了未知的实体参考

时间:2018-08-03 01:13:32

标签: spring hibernate jpa

我的项目正在使用JPA2 / hibernate将属性映射到它们各自的表。据我了解,我必须将属性mappedby放在所有者表中,并将JoinColumn放在子表中(许多方面)。我收到了如下所示的错误:

  

由以下原因引起:org.hibernate.AnnotationException:mappedBy引用了   目标实体属性未知:   ninja.familyhomestay.domain.HouseImage.homestay_info在   ninja.familyhomestay.domain.HomestayInfo.houseImages

这是我的HomestayInfo类:

@Entity
@Table(name = "homestay_info")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "homestay_info")
data class HomestayInfo(
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    var id: Long? = null,

    ...

    @OneToMany(mappedBy = "homestay_info", cascade = [CascadeType.ALL], fetch = FetchType.LAZY)
    var houseImages: MutableSet<HouseImage> = HashSet(),
...
) 

和我的houseImage类:

@Entity
@Table(name = "house_image")
@Document(indexName = "house_image")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
class HouseImage(
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    var id: Long? = null,
...
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "homestay_info_id")
    var homestayInfo: HomestayInfo? = null
) : Serializable 

有什么想法吗?我还使用Liquid在db中创建表。

1 个答案:

答案 0 :(得分:1)

在您的HomestayInfo类中,当提及@OneToMany关系时,您将mappedBy属性的值提到为homestay_info,而在HouseImage类中则没有名为homestay_info的字段。

您应该具有与namedBy中提到的字段名称相同的属性,并且该属性应在另一个类中指定双边关系。

在以下语句中将 homestayInfo 重命名为 homestay_Info

var homestayInfo: HomestayInfo? = null

mappedBy字段中的值重命名为 homestayInfo