我需要联系@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: tour.spring.bc.model.vo.Country.places
在餐桌城市和地区是外键的表国家(Region.country_id,City.country_id),这是可以的。但我不需要外键在表国家表,区域和城市,所以我不需要@JoinColum。
我能做什么?
发布于 2011-01-22 18:08:37
@Any在这里没有意义,因为外键位于Place的旁边,因此它不需要额外的元列。
我不确定是否有可能创建到@MappedSuperclass的多态关系。但是,您可以尝试将Place声明为@Entity @Inheritance(InheritanceType.TABLE_PER_CLASS),它应该生成相同的数据库模式,并允许多形性关系。
https://stackoverflow.com/questions/4769546
复制相似问题