У меня есть эти 4 таблицы на Oracle DB
FACTORY_MASTER
FACTORY_DETAILS
LOT_MASTER
LOT_DETAILS
Отношения
FACTORY_MASTER 1 - N FACTORY_DETAILS
LOT_MASTER 1 - N LOT_DETAILS
FACTORY_DETAILS 1 - 1 LOT_DETAILS
Это текущая реализация класса модели
public class FactoryMaster {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "factory_master_gen")
@SequenceGenerator(name = "factory_master_gen", sequenceName = "seq_factory_master")
@Column(name = "FACTORY_MASTER_ID")
private Long id;
@OneToMany(cascade = CascadeType.ALL,
orphanRemoval = true,
fetch = FetchType.LAZY)
@JoinColumn(name = "FACTORY_MASTER_ID",
referencedColumnName = "FACTORY_MASTER_ID",
nullable = false)
private List<FactoryDetail> details;
}
public class FactoryDetail {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "factory_detail_gen")
@SequenceGenerator(name = "factory_detail_gen", sequenceName = "seq_factory_detail")
@Column(name = "FACTORY_DETAIL_ID")
private Long id;
// I exclude on purpose ManyToOne, because you cannot have FactoryDetail without a Master
@OneToOne(mappedBy = "factoryDetail")
private LotDetail lotDetail;
}
public class LotMaster {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "lot_master_gen")
@SequenceGenerator(name = "lot_master_gen", sequenceName = "seq_lot_master")
@Column(name = "LOT_MASTER_ID")
private Long id;
@OneToMany(cascade = CascadeType.ALL,
orphanRemoval = true,
fetch = FetchType.LAZY)
@JoinColumn(name = "LOT_MASTER_ID",
referencedColumnName = "LOT_MASTER_ID",
nullable = false)
private List<LotDetail> details;
}
public class LotDetail {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "lot_detail_gen")
@SequenceGenerator(name = "lot_detail_gen", sequenceName = "seq_lot_detail")
@Column(name = "LOT_DETAIL_ID")
private Long id;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "FACTORY_DETAIL_ID",
referencedColumnName = "FACTORY_DETAIL_ID")
private FactoryDetail factoryDetail;
}
Мне нужно извлечь factory_master , используя lot_master_id
Таким образом, запрос будет
select distinct fm.*
from factory_master fm
join factory_details fd on fd.factory_master_id = fm.factory_master_id
join lot_details ld on fd.lot_details_id = ld.lot_details_id
join lot_master lt on ld.lot_master_id = lt.lot_master_id
where lt.lot_master_id = :ID
Как получить тот же результат через JPQL, действительный?
Спасибо