title: Spring Data JPA repository not fetching data in multiple datasource
body: <p>I am using Spring Data JPA and I have 3 entities and 2 datasource to fetch the data. One entity is connecting to one datasource and fetching the data correctly via JPA Repository. The problem is with 2nd entity. The two remaining entities are connected to 1 datasource via separate JPA Repository for each entity, but only one entity works here and NOT the other.</p>
  
  <p>While I am fetching the data from second entity, it simple shows the generated sql and no other error and results. Is there something am I missing ?</p>
  
  <p>My configuration files:</p>
  
  <pre><code>@Configuration
  @EnableJpaRepositories(entityManagerFactoryRef = "orderEntityManagerFactory",
          transactionManagerRef = "orderTransactionManager")
  public class OrderConfig {
  
  @Value("${spring.datasource.driver-class-name}")
  private String driverClass;
  
  @Value("${spring.datasource.url}")
  private String url;
  
  @Value("${spring.datasource.username}")
  private String username;
  
  @Value("${spring.datasource.password-key}")
  private String passwordKey;
  
  @Value("${spring.datasource.password-encrypted}")
  private String passwordEncrypted;
  
  @Bean
  PlatformTransactionManager orderTransactionManager() {
      return new JpaTransactionManager(orderEntityManagerFactory().getObject());
  }
  
  @Primary
  @PersistenceContext(unitName = "first")
  @Bean(name = "orderEntityManagerFactory")
  LocalContainerEntityManagerFactoryBean orderEntityManagerFactory() {
  
      HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
      jpaVendorAdapter.setGenerateDdl(true);
      jpaVendorAdapter.setShowSql(true);
  
      LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
  
      factoryBean.setDataSource(orderDataSource());
      factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
      factoryBean.setPackagesToScan(OrderConfig.class.getPackage().getName());
  
      return factoryBean;
  }
  
  
  @Bean(name = "orderDataSource")
  @Primary
  public DataSource orderDataSource() {
      HikariDataSource dataSource = new HikariDataSource();
      dataSource.setDriverClassName(driverClass);
      dataSource.setJdbcUrl(url);
      dataSource.setUsername(username);
      dataSource.setMaximumPoolSize(2);
      dataSource.setPassword(AESCryption.decrypt(passwordKey, passwordEncrypted));
      return dataSource;
  }
  </code></pre>
  
  <p>Another configuration file:</p>
  
  <pre><code>public class BoardRateConfig {
  @Value("${spring.datasource.driver-class-name-oracle}")
      private String driverClass;
  
      @Value("${spring.datasource.url-oracle}")
      private String url;
  
      @Value("${spring.datasource.username-oracle}")
      private String username;
  
      @Value("${spring.datasource.password-key-oracle}")
      private String passwordKey;
  
      @Value("${spring.datasource.password-encrypted-oracle}")
      private String passwordEncrypted;
  
      @Bean
      PlatformTransactionManager boardRateTransactionManager() {
          return new JpaTransactionManager(boardRateEntityManagerFactory().getObject());
      }
  
      @PersistenceContext(unitName = "second")
      @Bean(name = "boardRateEntityManagerFactory")
      LocalContainerEntityManagerFactoryBean boardRateEntityManagerFactory() {
  
          HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
          jpaVendorAdapter.setGenerateDdl(true);
          jpaVendorAdapter.setShowSql(true);
  
          LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
  
          factoryBean.setDataSource(boardRateDataSource());
          factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
          factoryBean.setPackagesToScan(BoardRateConfig.class.getPackage().getName());
  
          return factoryBean;
      }
  
  
      @Bean(name = "boardRateDataSource")
      public DataSource boardRateDataSource() {
          HikariDataSource dataSource = new HikariDataSource();
          dataSource.setDriverClassName(driverClass);
          dataSource.setJdbcUrl(url);
          dataSource.setUsername(username);
          dataSource.setMaximumPoolSize(2);
          dataSource.setPassword(AESCryption.decrypt(passwordKey, passwordEncrypted));
          return dataSource;
      }
  </code></pre>
  
  <p>My Entities:</p>
  
  <pre><code>@Getter
  @Setter
  @ToString
  @EqualsAndHashCode(of = {"orderId"})
  @Entity
  @Table(name = "customer_order", uniqueConstraints = { @UniqueConstraint(columnNames = { "transaction_id", "region" }) })
  @Data
  public class Order {
  
      @Id
      @GeneratedValue
      @Column(name = "order_id", columnDefinition="Unique Identifier of the Order")
      private Long orderId;
  
      @Column(name = "source_system")
      private String sourceSystem;
  
      @NotNull
      @Column(name = "region", nullable = false)
      @Enumerated(EnumType.STRING)
      private Region region;
  
      @NotNull
      @Column(name = "transaction_id", nullable = false)
      private String transactionId;
  
      @NotNull
      @Column(name = "dealt_currency", nullable = false)
      private String dealtCurrency;
  
      @NotNull
      @Column(name = "currency_pair", nullable = false)
      private String currencyPair;
  
      @Column(name = "dealth_amount", nullable = false)
      private BigDecimal dealtAmount;
  
      @NotNull
      @Column(name = "expected_rate", nullable = false)
      private BigDecimal expectedRate;
  
      @Column(name = "matched_rate", nullable = false)
      private BigDecimal matchedRate;
  
      @NotNull
      @Column(name = "request_type", nullable = false)
      @Enumerated(EnumType.STRING)
      private RequestType requestType;
  
      @NotNull
      @Column(name = "status", nullable = false)
      @Enumerated(EnumType.STRING)
      private Status status;
  
      @GridColumn(caption="Expiry Date (SGT)")
      @Column(name = "expiry_date")
      private LocalDateTime expiryDate;
  
      @GridColumn(caption="Created Date (SGT)")
      @Column(name = "created_date")
      private LocalDateTime createdDate;
  
      @GridColumn(caption="Matched Date Time (SGT)")
      @Column(name = "matched_date_time")
      private LocalDateTime matchedDateTime;
  
      @GridColumn(caption="Updated Date (SGT)")
      @Column(name = "updated_date")
      private LocalDateTime updatedDate;
  
      @GridColumn(caption="Expired Date Time (SGT)")
      @Column(name = "expired_date_time")
      private LocalDateTime expiredDateTime;
  
      @Column(name = "price_tier")
      private String priceTier;
  
      @NotNull
      @Column(name = "price_tier_currency", nullable = false)
      private String priceTierCurrency;
  
      @Version
      private Integer version;
  
      @Size(max = 1)
      @Column(name = "service_type")
      private String serviceType;
  
      public static enum Status {
          PENDING, MATCHED, EXPIRED, DELETED, NOTIFIED, ACKNOWLEDGED;
      }
  
      public static String getStatusValue(Status value){
          if(value == Status.ACKNOWLEDGED)
              return "ACKNOWLEDGED";
          if(value == Status.MATCHED)
              return "MATCHED";
          if(value == Status.EXPIRED)
              return "EXPIRED";
          if(value == Status.DELETED)
              return "DELETED";
          if(value == Status.NOTIFIED)
              return "NOTIFIED";
          if(value == Status.PENDING)
              return "PENDING";
  
          return null;
      }
  
      public static Stream&lt;Status&gt; statusStream(){
          return Arrays.stream(Status.values());
      }
  
      public static enum RequestType {
          BUY, SELL;
      }
  
      public static String getRequestTypeValue(RequestType value){
          if(value.equals(RequestType.BUY)){
              return "BUY";
          } else {
              return "SELL";
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>@Getter
  @Setter
  @ToString
  @EqualsAndHashCode(of = "orderId")
  @Entity
  @Table(name = "orders_match_at_board_rate_timestamp", uniqueConstraints = { @UniqueConstraint(columnNames = { "board_rate_timestamp", "order_id" }) })
  public class BoardRateTimeStamp {
  
      @Id
      @GeneratedValue
      @Column(name = "id", nullable = false)
      private Long id;
  
      @Column(name = "board_rate_timestamp", nullable = false)
      private LocalDateTime boardRateTimestamp;
  
      @Column(name = "order_id", columnDefinition="Unique Identifier of the Order")
      private Long orderId;
  
  }
  </code></pre>
  
  <hr>
  
  <p>Repositories:</p>
  
  <pre><code>    public interface BoardRateTimeStampRepository extends JpaRepository&lt;BoardRateTimeStamp, Long&gt; {
  
      @Query("SELECT brt FROM BoardRateTimeStamp brt WHERE brt.orderId IS NOT NULL AND brt.boardRateTimestamp &gt;= :fromDate AND brt.boardRateTimestamp &lt;= :toDate ")
      List&lt;BoardRateTimeStamp&gt; findMatchedOrdersBoardRateTimeStampListBetweenDateRange(@Param("fromDate") LocalDateTime fromDate, @Param("toDate") LocalDateTime toDate);
  }
  
  
  public interface OrderRepository extends JpaRepository&lt;Order, Long&gt; {
  
      @Query("SELECT o FROM Order o WHERE o.createdDate &gt;= :fromDate AND o.createdDate &lt;= :toDate")
      public List&lt;Order&gt; findOrderBetweenDateRangeForCreatedDate(@Param("fromDate") LocalDateTime fromDate, @Param("toDate") LocalDateTime toDate);
  
      @Query("SELECT o FROM Order o WHERE o.createdDate &gt;= :fromDate AND o.createdDate &lt;= :toDate AND o.status IN ( :status )")
      public List&lt;Order&gt; findOrderBetweenDateRangeForStatusForCreatedDate(@Param("fromDate") LocalDateTime fromDate, @Param("toDate") LocalDateTime toDate, @Param("status") EnumSet&lt;Order.Status&gt; status);
  
      @Query("SELECT o FROM Order o WHERE o.updatedDate &gt;= :fromDate AND o.updatedDate &lt;= :toDate")
      public List&lt;Order&gt; findOrderBetweenDateRangeForUpdatedDate(@Param("fromDate") LocalDateTime fromDate, @Param("toDate") LocalDateTime toDate);
  
      @Query("SELECT o FROM Order o WHERE o.updatedDate &gt;= :fromDate AND o.updatedDate &lt;= :toDate AND o.status IN ( :status )")
      public List&lt;Order&gt; findOrderBetweenDateRangeForStatusForUpdatedDate(@Param("fromDate") LocalDateTime fromDate, @Param("toDate") LocalDateTime toDate, @Param("status") EnumSet&lt;Order.Status&gt; status);
  
  
      @Query("SELECT o FROM Order o WHERE o.createdDate &gt;= :date")
      public List&lt;Order&gt; findOrdersCreatedToday(@Param("date") LocalDateTime date);
  
      @Query("SELECT o FROM Order o WHERE o.updatedDate &gt;= :date")
      public List&lt;Order&gt; findOrdersUpdatedToday(@Param("date") LocalDateTime date);
  
      @Query("SELECT o FROM Order o WHERE o.status = 'PENDING' ORDER BY o.currencyPair, o.priceTier ")
      public List&lt;Order&gt; findAllPendingOrders();
  </code></pre>
  
  <hr>
  
  <pre><code>public interface BoardRateRepository extends JpaRepository&lt;BoardRate, BigInteger&gt; {
  
      @Query("SELECT br FROM BoardRate br WHERE br.rateTypeId = 2 ORDER BY br.baseCurrency || br.quoteCurrency, br.regionTierId")
      public List&lt;BoardRate&gt; findLatestBoardRates();
  
  }
  </code></pre>
  
  <p>Here BoardRateRepository and OrderRepository are working fine but <strong>BoardRateTimeStampRepository</strong> is NOT working.</p>
  
  <p>When I make a call to <code>boardRateTimeStampRepository.findAll()</code> , I get empty list.</p>