David
David

Reputation: 4281

How to fetch or give query to get another table column to my table through in spring boot

How to fetch or give query to get another table column to my table through in spring boot.

I am doing some spring project and I have created two table here.

----------------------
|    Table 1        |
----------------------
 |  UserID          |
 |  InstrumenName   |
 |  Qty             |
 |  Price           |
 |  Date            |
----------------------


----------------------
|    Table 2        |
----------------------
 |  InstrumenName   |
 |  LTP             |
 |  Sector          |
----------------------

so while saving I am saving the Table data.

Here is my code for controller class.

@PostMapping("/employee")
    public Employee createEmployee(@RequestBody Employee employee) {
        return employeeRepository.save(employee);
    }

Here I am following all the rules like JPA repo, Model and COntroller.

Now when I wanted to get this data I wanted to add LTP from table 2 with respect to instrument name.

I am very new here so what I suppose to do. I have get code as well in my controller but I wanted to add LTP as well?

Do i need to write sql procedure or any businees login java code.

This would be my sql query :

SELECT table1.UserID, table1.Qty,table1.InstrumenName, table1.Price,table2.LTP             
FROM table1
INNER JOIN table2
ON table1.InstrumenName=table2.InstrumenName;

ANy help will be helpfull.

Upvotes: 0

Views: 1123

Answers (1)

Simon Martinelli
Simon Martinelli

Reputation: 36173

You need a class/interface to hold the result.

If it's read only you can use an interface if you also want to receive data then use a DTO (if using Java 16 a Record would be a great fit)

Please read the documentation about projections. https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections

Upvotes: 1

Related Questions