-1

How to fetch records using two table in JPA, there is not mapping between two tables. here is simple MySQL query only a column is common in both table that is, client_id as a destination

 select nc.* from Table1 n, Table2 nc
    where n.destination = nc.client_id and n.status = 'Created';

Thanks

shiva
  • 75
  • 1
  • 7
  • Does this answer your question? [How to use multiple JOIN FETCH in one JPQL query](https://stackoverflow.com/questions/30088649/how-to-use-multiple-join-fetch-in-one-jpql-query) – Kaustubh Khare Jul 06 '21 at 04:31
  • @KaustubhKhare No, because i don't have any mappings between two tables. – shiva Jul 06 '21 at 08:06

2 Answers2

0

try using @Query annotation to write native SQL queries. You can write it in an interface that's extending JPARepository

Like this:

> public interface TestRepository extends JPARepository {
>     @Query(value = "select nc.* from Table1 n, Table2 nc where n.destination = nc.client_id and n.status = 'Created'")
>     Collection<SomeClass> findAllClients(); }
0

JPA Repository is quite limited compared to the direct query to the database (at least from what I've seen while using it)

You can send a query directly from the Controller / where is your method using JdbcTemplate

// Autowiring JdbcTemplate so we can send queries to the database
@Autowired
JdbcTemplate jdbcTemplate;

public List<Map<String, Object>> mock_method() {

// Your query
String query = " select nc.* from Table1 n, Table2 nc
    where n.destination = nc.client_id and n.status = 'Created';";

return jdbcTemplate.queryForList(query);
}

This method will return a list of Map<String, Object>, where String is the name of the column, and Object is the value from that column (this way it can retrieve any type of data, then you can parse it to the needed type)

If there are no rows that match the query it will return nothing, so there is no need for error handling.