我正在尝试使用JPA本机查询,但没有得到预期的结果。
我尝试了懒惰和渴望的获取策略,但是问题是相同的。
public class OfferDetailEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "prod_spec_code")
private String productSpecificationCode;
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name = "offer_detail_id")
private Collection<ServiceEntity> services;
}
public class ServiceEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "service_name")
private String serviceName;
@Column(name = "service_id")
private Long serviceId;
}
动态生成的本地查询是
select * from offer_detail_entity detail inner join service_entity service
on service.offer_detail_id=detail.id where prod_spec_code = 'AT52476'
and service_name = 'Airtel'
它提供与OfferDetailEntity
相关的所有服务。
但是期望的服务只是具有service_name = 'Airtel'
的服务。
我尝试调试,发现休眠正在执行另一个查询。
我不确定为什么当我已经加入本机查询时,休眠状态又需要服务。
休眠产生的额外查询
select
services0_.offer_detail_id as offe4_11_0_,services0_.id as id1_11_0_,services0_.id as id1_11_1_,services0_.service_id as service_2_11_1_,services0_.service_name as service_3_11_1_
from
offer_service services0_
where`enter code here`
services0_.offer_detail_id=?
请为我提供解决方案。