【发布时间】:2021-04-03 04:24:16
【问题描述】:
我有两个实体 SuperAlbumEntity 和 AlbumEntity 反映同一个表“albums”。
超级专辑实体:
@Entity
@Table(name = "albums")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class SuperAlbumEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
//other fields
}
相册实体:
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "albums")
public class AlbumEntity extends SuperEntity{
//some fields
@Column(name = "country")
private String country;
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "country_name", referencedColumnName = "country")
private Set<CountryEntity> countrySet = new HashSet<>();
}
AlbumEntity 有 @OneToMany 映射到 CountryEntity:
@Entity
@Table(name = "countries")
public class CountryEntity implements Serializable {
@Id
String id;
String country_name;
//other fields
}
运行我的应用程序出现以下错误:
...
Caused by: org.hibernate.AnnotationException: referencedColumnNames(country) of CountryEntity.countrySet referencing AlbumEntity not mapped to a single property
...
有趣的是,如果我将 country 字段从 SuperAlbumEntity 移动到 AlbumEntity 一切正常... 谁能解释一下为什么会出现这个错误?
【问题讨论】:
-
我看到 countrySet 是一组 ContryEntity?这将是问题
-
是的,它是一组 CountryEntity,因为我有一个 @OneToMany 映射
-
@Omar Amaoun,如果我将“countrySet”更改为“country”,一切正常,但我不明白为什么......
标签: spring-boot spring-data-jpa spring-data hibernate-mapping