简体   繁体   中英

JPQL Query on repository for non-related entities

I am trying to make a JPQL request on a @Query from a repository(extends CrudRepository) for the following strucure:

@Entity 
@Table(name="Campaign")
public class Campaign {

 @Id
 @GeneratedValue(strategy=GenerationType.AUTO)
 @Column(columnDefinition = "int(11)")
 private Long id; 
...
 private String displayName;
 private String smrtProject;
...}

and

@Entity 
@Table(name="CampaignFact")
public class CampaignFact{
 @Id
 @GeneratedValue(strategy=GenerationType.AUTO)
 @Column(columnDefinition = "int(11)")
 private Long id;

 @ManyToOne(fetch = FetchType.EAGER)
 @JoinColumn(name = "campaignId")
 private Campaign campaign;

 @ManyToOne(fetch = FetchType.EAGER)
 @JoinColumn(name = "callCenterId")
 private CallCenter callCenter;
...}

What I am trying to query is fetching all campaigns related to a callcenter by callCenterId.

So far I am using a native query (MySql) that resumes well that I am try to do:

@Query(value = "SELECT c.* FROM CampaignFact cf Inner JOIN Campaign c ON c.id = cf.campaignId WHERE cf.callCenterId = ?1", nativeQuery = true)
List<Campaign> findByCallCenterIdNative(Long callCenterId);

So to resume how could re-write this SQL in JPQL? PS: I am working in this repository:

public interface CampaignRepository extends CrudRepository<Campaign, Long> 

Since you want CampaignFact so you need to have CampaignFactRepository and method definition will be like below:

List<CampaignFact> findByCampaign_idAndCallCenter_id(Long callCenterId);

I did not run it as I do not have DB but creating correct method name should make correct SQL call. Reference Spring Docs

Also you can check Spring Data JPA Tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM