开发者

How to join multiple tables and fetch results in spring data Jpa

开发者 https://www.devze.com 2022-12-07 18:28 出处:网络
I am fairly new to Spring Boot. I am trying to join a bunch of tables and get some data back. I have created the entities and repositories for the three tables I am working with. Below is the query I

I am fairly new to Spring Boot. I am trying to join a bunch of tables and get some data back. I have created the entities and repositories for the three tables I am working with. Below is the query I am trying to achieve.

           SELECT *
           FROM  PEFT_LINE PLI
           JOIN  PEFT_LINE_RELTN PLICR
            ON  PLICR.PEFT_LINE_ID   = PLI.PEFT_LINE_ID
           JOIN  PEFT_CHARGE PC
            ON  PC.PEFT_CHRG_ID      = PLICR.PEFT_CHRG_ID

I have created the entity classes for PEFT_LINE, PEFT_LINE_RELTN and PEFT_CHARGE where PEFT_LINE_RELTN contains the foreign key of both PEFT_LINE and PEFT_CHARGE, and I hav开发者_JAVA技巧e established the unidirectional mapping between the three tables.

Entity class for PEFT_LINE

                @Entity
                public class PEFT_LINE

                @Id
                private int PeftId;
                private int ProcCode;

                @OneToMany(targetEntity = PEFT_LINE_RELTN.class,cascade = CascadeType.ALL)
                @JoinColumn(name = "peft_line_id",referencedColumnName = "PeftId")
                private List<PEFT_LINE_RELTN> peft_line_reltn;

                ***getters and setters

Entity class for PEFT_LINE_RELTN

                @Entity
                public class PEFT_LINE_RELTN

                @Id
                private int PeftlineId;
                private int ActiveCd;

                ..getters and setters

Entity class for PEFT_CHARGE

                @Entity
                public class PEFT_CHARGE

                @Id
                private int PeftchrgId;
                private int ActiveCode;
                
                @OneToMany(targetEntity = PEFT_LINE_RELTN.class,cascade CascadeType.ALL)
                @JoinColumn(name = "peft_charge_id",referencedColumnName = "PeftchrgId")
                **getters and setters

PEFT_LINE Repository

                public interface PEFT_LINERepo extends JpaRepository<PeftId, Integer>{
                     
                }
               

Table schemas:

              PEFT_LINE : PeftId(Primary key)

              PEFT_LINE_RELTN : PeftlineId(Primary key) 
              peft_line_id(foreign key)
              peft_charge_id(foreign key)


              PEFT_CHARGE : PeftchrgId(foreign key)

Please let me know how can I achieve the above SQL query. Thanks in advance

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号