我需要联想 @OneToMany
从国家到超级类 Place
(@MappedSuperclass
). 它可以是双向的。我需要像 @OneToAny
.
@MappedSuperclass
public class Place {
private String name;
private Country country;
@Column
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne
@JoinColumn(name="country_id")
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
}
国家:
@Entity
public class Country {
private long id;
private String name;
private List<Place> places;
@Any(metaColumn = @Column(name = "place_type"), fetch = FetchType.EAGER)
@AnyMetaDef(idType = "integer", metaType = "string", metaValues = {
@MetaValue(value = "C", targetEntity = City.class),
@MetaValue(value = "R", targetEntity = Region.class) })
@Cascade({ org.hibernate.annotations.CascadeType.ALL })
//@JoinColumn(name="unnecessary")
//@OneToMany(mappedBy="country") // if this, NullPointerException...
public List<Place> getPlaces() {
return places;
}
//and rest of class
无 @JoinColunm
有一个异常。
原因是: org.hibernate.AnnotationException: @Any需要一个显式的@JoinColumn(s):tour.spring.bc.model.vo.Country.places。
在表City和Region中,外键是指向表Country的(Region.country_id, City.country_id),这是好的,但我不需要在表Country中的外键指向表Region和City,所以我不需要 @JoinColum
.
我可以做什么?
@Any
在这里是没有意义的,因为外键位于 Place
的一侧,因此它不需要额外的元列。
我不知道是否可以创建一个多态关系到 @MappedSuperclass
. 然而,你可以尝试声明 Place
作为 @Entity @Inheritance(InheritanceType.TABLE_PER_CLASS)
它应该产生相同的数据库模式,并允许多式关系。