Home » Java » Return Data of two tables using mysql join as one java Object in Spring boot-Exceptionshub

Return Data of two tables using mysql join as one java Object in Spring boot-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

CPT Table

@Entity
@Table(name = "CPT")
public class CPTCodeset {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "Id")
    private int id;

    @Column(name = "Code")
    private String code;

    @Column(name = "Description")
    private String desc;
//getters and setters
}

CPT Procedures Table

@Entity
@Table(name = "cptprocedures")
public class CPTProcedures {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "Id")
    private int Id;

    @OneToOne(fetch=FetchType.EAGER)
    @JoinColumn(name = "ProcedureId")
    private Procedure procedure;
    @Transient
    private int procedureId;

    @OneToOne(fetch=FetchType.EAGER)
    @JoinColumn(name = "CPTCodeSet")
    private CPTCodeset cpt;
    @Transient
    private int cptCodeSet;
//getters and setters
}

CPTRepository

@Query(nativeQuery = true, value = "SELECT cp.ProcedureId,c.Code,c.Description FROM cptprocedures cp JOIN CPT c ON cp.CPTCodeSet = c.Id where c.Description like \"%ane\";")
List<CPTCodeset> findBySearch(@Param("searchString") String searchString);

This Query Returning Data is what I want. But how can I map this data into a single object where as it includes 2 tables.

How can I do it by using @Entity without creating a table. Any suggestions?

How to&Answers:
@Query("SELECT new com.dto.ProduceCPT(cp.ProcedureId, c.Code, c.Description) FROM cptprocedures cp JOIN CPT c ON cp.CPTCodeSet = c.Id where c.Description like \"%ane\" ")
List< ProduceCPT> fetchProduceCPT();

Answer:

You’re doing it wrong if you are using JPA and still thinking in terms of tables first. The whole point of JPA is to have SQL generated from objects so you can pretend that the relational model doesn’t exist

For this example you can use a projection interface

That can solve your problem, you doing your projection, and later set the information into a WrapperObject. But you cant do a Wrapper in your JPARepository, if that wrapper doesn’t exists in your Database.

Answer:

You should replace desc field name. It’s better not to use JPA reserved words

@Entity
@Table(name = "CPT")
public class CPTCodeset {
    //fields

    @Column(name = "Description")
    private String description; 

    //getters and setters
}

To get query result as List<Object[]>

@Query("select cp.procedure.id, c.code, c.description "
         + "from CPTProcedures cp join cp.cpt c "
         + "where c.description like :searchString")
List<Object[]> findBySearch(@Param("searchString") String searchString);

or create DTO class with appropriate constructor SearchResultDto(int, String, String) and use this

@Query("select new yourproject.dto.SearchResultDto(cp.procedure.id, c.code, c.description) "
         + "from CPTProcedures cp join cp.cpt c "
         + "where c.description like :searchString")
List<SearchResultDto> findBySearch(@Param("searchString") String searchString);

Repository usage:

// description contains searchString  
List<SearchResultDto> dtos1 = repository.findBySearch("%" + searchString + "%");

// description start with searchString
List<SearchResultDto> dtos2 = repository.findBySearch("%" + searchString); 

// description end with searchString
List<SearchResultDto> dtos3 = repository.findBySearch(searchString + "%");