Reputation: 395
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>
Upvotes: 1
Views: 152
Reputation: 6818
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
Upvotes: 2