我正在尝试从存储库(扩展CrudRepository)对@Query发出JPQL请求以获取以下结构:
@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;
...}
和
@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;
...}
我想要查询的是通过callCenterId获取与callcenter相关的所有广告系列。
到目前为止,我使用的是原生查询(MySql),恢复得很好,我试图这样做:
@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);
那么要恢复如何在JPQL中重写这个SQL? PS:我在这个仓库工作:
public interface CampaignRepository extends CrudRepository<Campaign, Long>
答案 0 :(得分:2)
由于您需要CampaignFact
,因此您需要CampaignFactRepository
,方法定义如下:
List<CampaignFact> findByCampaign_idAndCallCenter_id(Long callCenterId);
我没有运行它,因为我没有DB但是创建正确的方法名称应该进行正确的SQL调用。参考Spring Docs
您也可以查看Spring Data JPA Tutorial