JpaRepository - 内部连接 - 后续选择
JpaRepository - Inner Join - Subsequent selects
当我尝试获取 ServiceCup 列表及其 ServiceLanguage 时遇到问题。当我尝试在我的服务层中操作 ServiceCup 列表时,hibernate 正在执行第二个查询并再次使用所有 ServiceLanguage 填充我的 ServiceCup。
ServiceCup x ServiceLanguage x LanguageCup
服务杯:
@Data
@Entity
@Table(name = "csm_service")
public class ServiceCup extends BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
private String context;
// bi-directional many-to-one association to CsmServiceLanguage
@OneToMany(mappedBy = "service")
private List<ServiceLanguage> serviceLanguages;
}
服务语言:
@Data
@Entity
@Table(name = "csm_service_language")
public class ServiceLanguage extends BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "translated_name")
private String translatedName;
// bi-directional many-to-one association to CsmLanguage
@ManyToOne
@JoinColumn(name = "csm_language_id_fk")
private LanguageCup language;
// bi-directional many-to-one association to CsmService
@ManyToOne
@JoinColumn(name = "csm_service_id_fk")
private ServiceCup service;
}
JpaRepository:
@Query(value = "select s, sl from ServiceCup s \n" + "INNER JOIN FETCH ServiceLanguage sl on s.id = sl.service \n"
+ "where sl.language.id = :languageId")
List<ServiceCup> findAllServicesByLanguageId(@Param("languageId") String languageId);
存储层查询:
select *all_fields* from csm_service servicecup0_ inner join csm_service_language servicelan1_ on (servicecup0_.id=servicelan1_.csm_service_id_fk) where servicelan1_.csm_language_id_fk=?
但是在服务层执行了很多查询,把ServiceCup的所有关系都带过来了。我想要填充 ServiceCup 对象,但只填充查询中的结果。
如何获取只有查询结果的ServiceCup对象?
PS:在我的服务层方法中,我有 @Transactional(readOnly = true) 但是如果我删除我无法获得与 ServiceCup 相关的对象.
我需要使用投影而不是实体来只执行一次查询。
ServiceCupProj
public interface ServiceCupProj {
public Long getId();
public String getDescription();
public String getInternalname();
......
存储库
@Repository
public interface CupServiceRepository extends JpaRepository<ServiceCup, Long> {
@Query(value = "select servicecup0_.id as id, servicelan1_.translated_description as description, servicelan1_.translated_name as internalname \n"
+ "from csm_service servicecup0_ inner join csm_service_language servicelan1_ on (servicecup0_.id=servicelan1_.csm_service_id_fk) \n"
+ "where servicelan1_.csm_language_id_fk = :languageId ", nativeQuery = true)
List<ServiceCupProj> findAllServicesByLanguageId(@Param("languageId") String languageId);
......
并且在服务层中,我将这个 ServiceCupProj 转换为我需要的实体。
当我尝试获取 ServiceCup 列表及其 ServiceLanguage 时遇到问题。当我尝试在我的服务层中操作 ServiceCup 列表时,hibernate 正在执行第二个查询并再次使用所有 ServiceLanguage 填充我的 ServiceCup。
ServiceCup x ServiceLanguage x LanguageCup
服务杯:
@Data
@Entity
@Table(name = "csm_service")
public class ServiceCup extends BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
private String context;
// bi-directional many-to-one association to CsmServiceLanguage
@OneToMany(mappedBy = "service")
private List<ServiceLanguage> serviceLanguages;
}
服务语言:
@Data
@Entity
@Table(name = "csm_service_language")
public class ServiceLanguage extends BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "translated_name")
private String translatedName;
// bi-directional many-to-one association to CsmLanguage
@ManyToOne
@JoinColumn(name = "csm_language_id_fk")
private LanguageCup language;
// bi-directional many-to-one association to CsmService
@ManyToOne
@JoinColumn(name = "csm_service_id_fk")
private ServiceCup service;
}
JpaRepository:
@Query(value = "select s, sl from ServiceCup s \n" + "INNER JOIN FETCH ServiceLanguage sl on s.id = sl.service \n"
+ "where sl.language.id = :languageId")
List<ServiceCup> findAllServicesByLanguageId(@Param("languageId") String languageId);
存储层查询:
select *all_fields* from csm_service servicecup0_ inner join csm_service_language servicelan1_ on (servicecup0_.id=servicelan1_.csm_service_id_fk) where servicelan1_.csm_language_id_fk=?
但是在服务层执行了很多查询,把ServiceCup的所有关系都带过来了。我想要填充 ServiceCup 对象,但只填充查询中的结果。
如何获取只有查询结果的ServiceCup对象?
PS:在我的服务层方法中,我有 @Transactional(readOnly = true) 但是如果我删除我无法获得与 ServiceCup 相关的对象.
我需要使用投影而不是实体来只执行一次查询。
ServiceCupProj
public interface ServiceCupProj {
public Long getId();
public String getDescription();
public String getInternalname();
......
存储库
@Repository
public interface CupServiceRepository extends JpaRepository<ServiceCup, Long> {
@Query(value = "select servicecup0_.id as id, servicelan1_.translated_description as description, servicelan1_.translated_name as internalname \n"
+ "from csm_service servicecup0_ inner join csm_service_language servicelan1_ on (servicecup0_.id=servicelan1_.csm_service_id_fk) \n"
+ "where servicelan1_.csm_language_id_fk = :languageId ", nativeQuery = true)
List<ServiceCupProj> findAllServicesByLanguageId(@Param("languageId") String languageId);
......
并且在服务层中,我将这个 ServiceCupProj 转换为我需要的实体。