Search code examples
spring-bootjpaspring-data-jpa

Fetch field data by joining tables in spring data JPA


I have 3 tables using spring data JPA. Table C is the mapping of Table A & B.

Table A Table B Table C
id id idA
name tname idB
code tcode Sname

In front end side I want to populate a table with the values from table C. Instead of column IDs (idA & idB) I want to show the name from respective tables. In front end I want to show as:

Name Type Subtype
Name A1 TName B1 Sname C1
Name A2 TName B2 Sname C2

Using spring boot how can I get the values from other tables and how can perform

  1. a select query
  2. Using stored procedure how can I perform this operation. Note: I know the SQL query part not sure how to it properly in spring data jpa.

Solution

  • For the JPA mappings take a look at this.

    The query to obtain what you want based on the above link would be:

    @Query("select b.title, bp.format, p.name from Book b join b.bookPublisher bp join bp.publisher p")
    

    Creating a stored procedure will differ based on the database used. Once the procedure is defined this will help you to wire JPA to call the stored procedure and obtain the results.