I am recreating some old Java code and remaking it with Spring Boot. I am attempting to recreate this query using the JPA interface commands but I'm getting stumped because of the 'join'.
The original query is:
select pc.procedureCodeId, PC.AMOUNTTYPE from Section s join SectionContents sc
on sc.sectionTitle = s.sectionTitle and sc.cdtVersionId = s.cdtVersionId
join ProcedureCode pc on pc.procedureCodeId = sc.procedureCodeId and
pc.cdtVersionId = sc.cdtVersionId and pc.benefitId = ? where
s.sectionTitle = ? and s.cdtVersionId = ?
In the remake I have created 3 entities:
SectionEntity
@Entity
@Table(name = "SECTION", schema = "BPDMOWNER", catalog = "")
@IdClass(SectionEntityPK.class)
public class SectionEntity {
private String sectiontitle;
private long cdtversionid;
private String filingid;
private String sectiondescription;
private String defaultplanname;
private Timestamp lastupdate;
private String lastupdatedby;
private String sectionheading;
private String schedule;
private String eocfilingid;
@Id
@Column(name = "SECTIONTITLE", nullable = false, length = 30)
public String getSectiontitle() {
return sectiontitle;
}
SectioncontentEntity
@Entity
@Table(name = "SECTIONCONTENTS", schema = "BPDMOWNER", catalog = "")
@IdClass(SectioncontentsEntityPK.class)
public class SectioncontentsEntity {
private String sectiontitle;
private long cdtversionid;
private long sequence;
private String amounttext;
private String amounttextspanish;
private String amounttype;
private Byte leaderline;
private Timestamp lastupdate;
private String lastupdatedby;
private Long intoc;
private Byte eocleaderline;
private SectionEntity section;
@Id
@Column(name = "SECTIONTITLE", nullable = false, length = 30)
public String getSectiontitle() {
return sectiontitle;
}
ProcedurecodeEntity
@Entity
@Table(name = "PROCEDURECODE", schema = "BPDMOWNER", catalog = "")
public class ProcedurecodeEntity {
private long procedurecodeid;
private String procedurecode;
private String proceduredescription;
private String proceduredescriptionspanish;
private String proceduredescriptiondbb;
private String amounttext;
private String amounttextspanish;
private String amounttype;
private String procedurecodecomment;
private String procedurecoderemark;
private Timestamp lastupdate;
private String lastupdatedby;
private Long benefitid;
@Id
@Column(name = "PROCEDURECODEID", nullable = false, precision = 0)
public long getProcedurecodeid() {
return procedurecodeid;
}
If anyone could help me out in figuring out how to implement this query using the @Entity classes and the @Repository interface.
Thank you in advance.
First you have to create the association between your entities to make your Jpa repository query work.
SectionEntity
@Entity
@Table(name = "SECTION", schema = "BPDMOWNER", catalog = "")
@IdClass(SectionEntityPK.class)
public class SectionEntity {
private String sectiontitle;
private long cdtversionid;
private String filingid;
private String sectiondescription;
private String defaultplanname;
private Timestamp lastupdate;
private String lastupdatedby;
private String sectionheading;
private String schedule;
private String eocfilingid;
@OneToOne
@JoinColumn(name="section_contents_cdtversionid") // just your column name for the association
private SectionContentsEntity sectionContent;
@Id
@Column(name = "SECTIONTITLE", nullable = false, length = 30)
public String getSectiontitle() {
return sectiontitle;
}
SectionContentsEntity
@Entity
@Table(name = "SECTIONCONTENTS", schema = "BPDMOWNER", catalog = "")
@IdClass(SectioncontentsEntityPK.class)
public class SectioncontentsEntity {
private String sectiontitle;
private long cdtversionid;
private long sequence;
private String amounttext;
private String amounttextspanish;
private String amounttype;
private Byte leaderline;
private Timestamp lastupdate;
private String lastupdatedby;
private Long intoc;
private Byte eocleaderline;
@OneToOne(mappedBy="sectionContent")
private SectionEntity section;
@OneToOne
@JoinColumn(name="procedure_code_id") // just your column name for the association
private ProcedurecodeEntity procedureCode;
@Id
@Column(name = "SECTIONTITLE", nullable = false, length = 30)
public String getSectiontitle() {
return sectiontitle;
}
ProcedurecodeEntity
@Entity
@Table(name = "PROCEDURECODE", schema = "BPDMOWNER", catalog = "")
public class ProcedurecodeEntity {
private long procedurecodeid;
private String procedurecode;
private String proceduredescription;
private String proceduredescriptionspanish;
private String proceduredescriptiondbb;
private String amounttext;
private String amounttextspanish;
private String amounttype;
private String procedurecodecomment;
private String procedurecoderemark;
private Timestamp lastupdate;
private String lastupdatedby;
private Long benefitid;
@OneToOne(mappedBy="procedureCode")
private SectionContent sectionContent;
@Id
@Column(name = "PROCEDURECODEID", nullable = false, precision = 0)
public long getProcedurecodeid() {
return procedurecodeid;
}
If your association is not OneToOne or bidirectional just change that to your needs.
Then in your jpa query you should use it like this:
public interface ProcedurecodeEntityRepository extends JpaRepository<ProcedurecodeEntity, Integer> {
@Query("select pc.procedurecodeid, pc.amounttype from ProcedurecodeEntity pc " +
"join pc.sectionContent sc " +
"join pc.sectionContent.section s " +
"where pc.benefitid = ?1 " +
"and s.sectiontitle = ?2 " +
"and s.cdtversionid = ?3")
ProcedurecodeEntity findByBenefitIdAndSectionTitleAndCdtVersionId(long benefitid, String sectiontitle, long cdtversionid);
}