0

I want to execute the same query on different database based on the request mapping url in controller .

Below is code snippet from my service impl class.

@Service("metricsService")
public class MetricsServiceImpl implements MetricsService {
    
    @PersistenceContext
    private EntityManager entityManager;
    
   String sql_query = "Select count(*) from user_details";

    public int countWPReqRaisedWithinDateRange(Date fromDate,Date toDate) {
        Query query = entityManager.createNativeQuery(sql_query);
        query.setParameter("fromDate", fromDate);
        query.setParameter("toDate",toDate);
        int count = ((Number) query.getSingleResult()).intValue();
        return count;
    }

}

I want to execute the same sql_query on different database based on the request mapping url.How can i customise this injected entity manager to execute on different database??

Gitesh
  • 346
  • 2
  • 4
  • 14
  • 1
    Hope this can help you. https://stackoverflow.com/questions/68032129/how-do-i-write-in-two-different-databases-in-spring-boot https://stackoverflow.com/questions/56968464/configuring-multiple-databases-in-spring-boot – ParthPatel Jan 04 '22 at 11:39
  • I don't have any JPARepository to enable , nor any entities created.I am just using native SQL Query ,in such case what should i give in packages() – Gitesh Jan 04 '22 at 12:23
  • This can help you. https://stackoverflow.com/questions/43509145/spring-boot-multiple-data-sources-using-entitymanager – ParthPatel Jan 04 '22 at 12:47
  • You cannot use JPA without entities. If you just need to execute a query use plain JDBC, through a `JdbcTemplate`. – M. Deinum Jan 04 '22 at 13:07

0 Answers0