我有两个实体SuperAlbumEntity和AlbumEntity反映同一个表"相册">。
超级相册实体:
@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,一切都会很好。。。有人能解释一下我为什么会犯这个错误吗?
我不确定,但我认为这与你使用它的固有类型有关。试着将你的超类修改为这样的东西:
超级相册实体:
@MappedSuperclass
public abstract class SuperAlbumEntity {
}
相册实体:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Table(name = "albums")
public class AlbumEntity extends SuperEntity {
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "country_name", referencedColumnName = "country")
private Set<CountryEntity> countrySet = new HashSet<>();
}