3

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?

1
  • Could you show what you have attempted so far? Also it would be a good idea to show the Entity you are trying to map to Commented Feb 19, 2020 at 7:52

3 Answers 3

6
@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();
Sign up to request clarification or add additional context in comments.

6 Comments

So this ProduceCPT is acts like a Wrapper class right
ProduceCPT is a wrapper class
be sure of your constructor plz refer to this link: smarterco.de/spring-data-jpa-query-result-to-dto
The Description is TEXT type in mySql Data Table
I have created Parameterized constructor but then also it is saying same cptprocedures not mapped
|
0

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.

1 Comment

Yeah Thats what i'm thinking but i didn't getting idea about how to do it
0

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 + "%");

1 Comment

I tried with DTO but is showing error like cptprocedures is not mapped

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.