我有以下映射的超类,可作为其他3个类的超类:
@MappedSuperclass
public abstract class Donation {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
//other irrelevant attributes
@JsonIgnore
@OneToMany(fetch = FetchType.LAZY)
private List<Voucher> vouchers;
//getters and setters
}
从Donation
扩展的其他类是Adoption
,Appointment
和Exam
。这种关系为Voucher
创建了以下关联实体:adoption_vouchers
,appointment_vouchers
和exam_vouchers
。
当前的问题是,现在我必须列出所有Voucher
及其Donation
实体。 由于我没有在凭证类别中指定捐赠关系(因为它可以表示所提及的3个类别中的任何一个,并且我与@MappedSuperclass
无关),是否存在使用HQL(检索类似Map<Donation, Voucher>
之类的方法)还是为了获取此信息而对我的结构进行细微更改?
答案 0 :(得分:1)
HQL在以下模型中可能看起来像这样
SELECT a.vouchers FROM Adoption a;
public enum DonationType {
ADOPTION, APPOINTMENT, EXAM;
}
@MappedSuperclass
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type",
discriminatorType = DiscriminatorType.STRING)
public abstract class Donation {
@Enumerated(EnumType.STRING)
private DonationType type;
@JsonIgnore
@OneToMany(fetch = FetchType.LAZY)
private List<Voucher> vouchers;
}
@Entity
@DiscriminatorValue(DonationType.ADOPTION)
public class Adoption extends Donation {
}
@Entity
@DiscriminatorValue(DonationType.Exam)
public class Exam extends Donation {
}
@Entity
@DiscriminatorValue(DonationType.APPOINTMENT)
public class Appointment extends Donation {
}
@Entity
public class Voucher {
//could be single mapping just to supperclass, depends on logic what do you need
@ManyToOne
private Adoption adoption;
@ManyToOne
private Exam exam;
@ManyToOne
private Appointment exam;
}