@MappedSuperClass und @onetomany
-
22-10-2019 - |
Frage
Ich brauche eine Onetomany von Land zu Superclass Place (@mappedsuperClass). Es könnte bidirektional sein. Ich brauche so etwas wie @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;
}
}
Land:
@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
Ohne @Joincolunm gibt es eine Ausnahme
Caused by: org.hibernate.AnnotationException: @Any requires an explicit @JoinColumn(s): tour.spring.bc.model.vo.Country.places
In Table City und Region ist der ausländische Schlüssel zum Tischland (Region.country_id, City.country_id), was in Ordnung ist. Aber ich brauche keinen ausländischen Schlüssel in der Region Tisch und Stadt in Table Country to Tischen, also brauche ich nicht @Joincolum.
Ich habe viel nach Lösung gesucht, aber es scheint, dass es keine gute Lösung gibt.
Lösung
@Any
macht hier keinen Sinn, da der Fremdkey bei der ist Place
s Seite und daher benötigt es keine zusätzliche Meta -Säule.
Ich bin mir nicht sicher, ob es möglich ist, eine polymorphe Beziehung zu schaffen @MappedSuperclass
. Sie können jedoch versuchen zu deklarieren Place
wie @Entity @Inheritance(InheritanceType.TABLE_PER_CLASS)
, Es sollte das gleiche Datenbankschema herstellen und eine polymorpische Beziehung zulassen.