Spring Data JPA map the stored procedure result to Non-Entity POJO from multiple data source

blackbird

I have tried to research on a better way of calling two stored procedures on two different data sources with no luck. Here is my scenario, I have connection to two data sources, each data source with a stored procedure. whenever I annotate one configuration as primary I get the results, the other configuration which is not mapped as primary throws an error

The given SqlResultSetMapping name [Unknown SqlResultSetMapping [Testing]] is unknown

When I set the other configuration for the class calling the stored procedure as primary, It shows results, but when both of them are not set as primary they throw an error

The given SqlResultSetMapping name [Unknown SqlResultSetMapping [Testing]] is unknown

Been stuck for several days. I will appreciate any help.Thank you in advance

my first config for calling the first data source

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "guru.springframework.multipledatasources.model.Card",
        entityManagerFactoryRef = "cardEntityManagerFactory",
        transactionManagerRef= "cardTransactionManager")
public class Cardonfiguration {

        @Bean
        @Primary
        @ConfigurationProperties("datasource.card")
        public DataSourceProperties cardDataSourceProperties() {
            return new DataSourceProperties();
        }

        @Bean
        @Primary
        @ConfigurationProperties("datasource.card.configuration")
        public DataSource cardDataSource() {
            return cardDataSourceProperties().initializeDataSourceBuilder()
                    .type(BasicDataSource.class).build();
        }

        @Primary
        @Bean(name = "cardEntityManagerFactory")
        public LocalContainerEntityManagerFactoryBean cardEntityManagerFactory(
                EntityManagerFactoryBuilder builder) {
            return builder
                    .dataSource(cardDataSource())
                    .packages(Card.class)
                    .build();
        }

        @Primary
        @Bean
        public PlatformTransactionManager cardTransactionManager(
                final @Qualifier("cardEntityManagerFactory") LocalContainerEntityManagerFactoryBean cardEntityManagerFactory) {
            return new JpaTransactionManager(Objects.requireNonNull(cardEntityManagerFactory.getObject()));
    }
}

my second config for the second data source

@Configuration
@EnableJpaRepositories(basePackages = "guru.springframework.multipledatasources.model.member",
        entityManagerFactoryRef = "memberEntityManagerFactory",
        transactionManagerRef= "memberTransactionManager")
public class MemberConfiguration {
    @Bean
    @ConfigurationProperties("datasource.member")
    public DataSourceProperties zamphiaDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties("datasource.member.configuration")
    public DataSource memberDataSource() {
        return memberDataSourceProperties().initializeDataSourceBuilder()
                .type(BasicDataSource.class).build();
    }

    @Bean(name = "memberEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean memberEntityManagerFactory(
            EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(memberDataSource())
                .packages(member.class)
                .build();
    }

    @Bean
    public PlatformTransactionManager memberTransactionManager(
            final @Qualifier("memberEntityManagerFactory") LocalContainerEntityManagerFactoryBean memberEntityManagerFactory) {
        return new JpaTransactionManager(memberEntityManagerFactory.getObject());
    }

}

my controller for the first data source

@RequestMapping("/api/v1")
@RestController
public class CardReports {

    @PersistenceContext
    private EntityManager entityManager;

    @SuppressWarnings("unchecked")
    @RequestMapping("/reports/CARD/dailyreport")
    public List<Testing> getDailyAchievements(){
        List<Testing> list;
        StoredProcedureQuery query = entityManager.createStoredProcedureQuery("dailyreports", "Testing");
        try {
            // Execute query
            query.execute();
            list = query.getResultList();
        } finally {
            try {
                query.unwrap(ProcedureOutputs.class).release();
            } catch (Exception e) {
            }
        }
        return list;
    }
}

my non entity for the first data source

@MappedSuperclass
@SqlResultSetMapping(name = "Testing", classes = @ConstructorResult(targetClass = Testing.class, columns = {
        @ColumnResult(name = "TTEAM_ID", type = Integer.class),
        @ColumnResult(name = "VDATE", type = String.class),
        @ColumnResult(name="Days_in_Field", type = String.class),
        @ColumnResult(name= "Expected_cummilative_Number_of_HH", type = String.class),
        @ColumnResult(name="Cummlative_HH_Reached", type = String.class),
        @ColumnResult(name="HH_Reached_for_day", type = String.class),
        @ColumnResult(name ="HHs_Finalised_for_day", type = String.class),
        @ColumnResult(name="Number_of_HH_in_process_for_day", type = String.class),
        @ColumnResult(name="Number_of_HH_Refused_for_day", type = String.class),
        @ColumnResult(name="Number_of_Revisits_Scheduled_for_day", type = String.class),
        @ColumnResult(name="HH_loss_3rd_visit_attempt_refusals_for_day", type = String.class)
}))

public class Testing {
    private Integer TTEAM_ID;
    private String  VDATE;
    private String Days_in_Field;
    private String Expected_cummilative_Number_of_HH;
    private String Cummlative_HH_Reached;
    private String HH_Reached_for_day;
    private String HHs_Finalised_for_day;
    private String Number_of_HH_in_process_for_day;
    private String Number_of_HH_Refused_for_day;
    private String Number_of_Revisits_Scheduled_for_day;
    private String HH_loss_3rd_visit_attempt_refusals_for_day;

    public Testing(Integer TTEAM_ID, String VDATE, String Days_in_Field, String Expected_cummilative_Number_of_HH, String Cummlative_HH_Reached, String HH_Reached_for_day, String HHs_Finalised_for_day, String Number_of_HH_in_process_for_day, String Number_of_HH_Refused_for_day, String Number_of_Revisits_Scheduled_for_day, String HH_loss_3rd_visit_attempt_refusals_for_day) {
        this.TTEAM_ID = TTEAM_ID;
        this.VDATE = VDATE;
        this.Days_in_Field = Days_in_Field;
        this.Expected_cummilative_Number_of_HH = Expected_cummilative_Number_of_HH;
        this.Cummlative_HH_Reached = Cummlative_HH_Reached;
        this.HH_Reached_for_day = HH_Reached_for_day;
        this.HHs_Finalised_for_day = HHs_Finalised_for_day;
        this.Number_of_HH_in_process_for_day= Number_of_HH_in_process_for_day;
        this.Number_of_HH_Refused_for_day = Number_of_HH_Refused_for_day;
        this.Number_of_Revisits_Scheduled_for_day = Number_of_Revisits_Scheduled_for_day;
        this.HH_loss_3rd_visit_attempt_refusals_for_day = HH_loss_3rd_visit_attempt_refusals_for_day;
    }
//getters and setters

my second data source controller

 @RequestMapping("/api/v1")
    @RestController
    public class MemberDaily {
    
        @PersistenceContext
        private EntityManager entityManager;
    
        @SuppressWarnings("unchecked")
        @RequestMapping("/reports/member/dailyachievementreport")
        public List<DailyReport> getDailyAchievements(){
            List list;
            StoredProcedureQuery query = entityManager.createStoredProcedureQuery("dailyreports", "Testing");
            try {
                // Execute query
                query.execute();
                list = query.getResultList();
            } finally {
                try {
                    query.unwrap(ProcedureOutputs.class).release();
                } catch (Exception e) {
                }
            }
            return list;
        }
    }

how I am mapping results on my pojo for my second data source

@MappedSuperclass
    @SqlResultSetMapping(name = "Testing", classes = @ConstructorResult(targetClass = Testing.class, columns = {
            @ColumnResult(name = "TTEAM_ID", type = Integer.class),
            @ColumnResult(name = "VDATE", type = String.class),
            @ColumnResult(name="Days_in_Field", type = String.class),
            @ColumnResult(name= "Expected_cummilative_Number_of_HH", type = String.class),
            @ColumnResult(name="Cummlative_HH_Reached", type = String.class),
            @ColumnResult(name="HH_Reached_for_day", type = String.class),
            @ColumnResult(name ="HHs_Finalised_for_day", type = String.class),
            @ColumnResult(name="Number_of_HH_in_process_for_day", type = String.class),
            @ColumnResult(name="Number_of_HH_Refused_for_day", type = String.class),
            @ColumnResult(name="Number_of_Revisits_Scheduled_for_day", type = String.class),
            @ColumnResult(name="HH_loss_3rd_visit_attempt_refusals_for_day", type = String.class)
    }))
    
    public class Testing {
        private Integer TTEAM_ID;
        private String  VDATE;
        private String Days_in_Field;
        private String Expected_cummilative_Number_of_HH;
        private String Cummlative_HH_Reached;
        private String HH_Reached_for_day;
        private String HHs_Finalised_for_day;
        private String Number_of_HH_in_process_for_day;
        private String Number_of_HH_Refused_for_day;
        private String Number_of_Revisits_Scheduled_for_day;
        private String HH_loss_3rd_visit_attempt_refusals_for_day;
    
        public Testing(Integer TTEAM_ID, String VDATE, String Days_in_Field, String Expected_cummilative_Number_of_HH, String Cummlative_HH_Reached, String HH_Reached_for_day, String HHs_Finalised_for_day, String Number_of_HH_in_process_for_day, String Number_of_HH_Refused_for_day, String Number_of_Revisits_Scheduled_for_day, String HH_loss_3rd_visit_attempt_refusals_for_day) {
            this.TTEAM_ID = TTEAM_ID;
            this.VDATE = VDATE;
            this.Days_in_Field = Days_in_Field;
            this.Expected_cummilative_Number_of_HH = Expected_cummilative_Number_of_HH;
            this.Cummlative_HH_Reached = Cummlative_HH_Reached;
            this.HH_Reached_for_day = HH_Reached_for_day;
            this.HHs_Finalised_for_day = HHs_Finalised_for_day;
            this.Number_of_HH_in_process_for_day= Number_of_HH_in_process_for_day;
            this.Number_of_HH_Refused_for_day = Number_of_HH_Refused_for_day;
            this.Number_of_Revisits_Scheduled_for_day = Number_of_Revisits_Scheduled_for_day;
            this.HH_loss_3rd_visit_attempt_refusals_for_day = HH_loss_3rd_visit_attempt_refusals_for_day;
        }
    //getters and setters
Chinna T

Your cardEntityManagerFactory will scan the package of Card.class for classes annotated with @ManagedSuperClass and memberEntityManagerFactory will scan the package of member.class

It looks like you are using the same POJO with SQLResultSetMapping "Testing" in both the controllers. But it is not clear in which package this POJO is. So make sure the package of your POJO is scanned by both the entity managers.

Also, You are simply auto wiring the entity manager without specifying name in both the controllers. As per my understanding, only the Primary entity manager will be autowired in both the controllers if you don't specify a Qualifier.

I think, you can try with @PersistenceContext(name = "cardEntityManagerFactory") and @PersistenceContext(name = "memberEntityManagerFactory") in the CardReports and MemberDaily controllers respectively.

Update

I had a look at your GitHub code and found that your BAISVReports controller was getting the primary entity manager only. So just autowired the entitymanager using @Qualifier as shown below and now the secondary entity manager is getting autowired. Now both controllers are able to find the SQLResultSetMapping

@RequestMapping("/api/v1")
@RestController
public class BAISVReports {

@Autowired
@Qualifier("baisvEntityManagerFactory")
private EntityManager entityManager;

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Dapper Call stored procedure and map result to class

分類Dev

How to select entities by calling a stored procedure with Spring Data

分類Dev

How to select entities by calling a stored procedure with Spring Data

分類Dev

Spring Data JPA - The statement did not return a result set

分類Dev

Spring Data JPA - The statement did not return a result set

分類Dev

Spring Data JPA - The statement did not return a result set

分類Dev

execute stored procedure in entity Framework Core without expecting map to dbset

分類Dev

Spring Data JPA @OneToMany

分類Dev

Retrieve output from stored procedure result in R with odbc/DBI

分類Dev

T-SQL stored procedure to get data from any table on the server for CSV export (SQL Server 2016)

分類Dev

try to render data from 2 different source using .map() function

分類Dev

Spring Data JPA Closed Connection

分類Dev

Spring Data JPA Lazy Loading

分類Dev

Spring Jpa Data、Pageable、Pagerequest

分類Dev

Spring Jpa Data、Pageable、Pagerequest

分類Dev

Spring Data JPA - Specifications and Querydsl

分類Dev

Spring Data JPA + JpaSpecificationExecutor + NamedEntityGraph

分類Dev

Data JPA Test with reactive spring

分類Dev

Spring data jpa limit not working

分類Dev

Spring Data JPA + Hibernate + PostgreSQL

分類Dev

Netezza - How to return multiple values from a stored procedure without REFTABLE

分類Dev

How to return multiple values from stored procedure within tsql query

分類Dev

SQL Server : 2 Stored Procedure 1 for XML & one for data

分類Dev

Pull a set of data through stored procedure and send an email

分類Dev

Search stored procedure returns cut off column data

分類Dev

Is possible map a single entity with multiple tables using JPA?

分類Dev

Spring + hibernate versus Spring Data JPA: Are they different?

分類Dev

Spring + hibernate versus Spring Data JPA: Are they different?

分類Dev

Spring + hibernate versus Spring Data JPA: Are they different?

Related 関連記事

  1. 1

    Dapper Call stored procedure and map result to class

  2. 2

    How to select entities by calling a stored procedure with Spring Data

  3. 3

    How to select entities by calling a stored procedure with Spring Data

  4. 4

    Spring Data JPA - The statement did not return a result set

  5. 5

    Spring Data JPA - The statement did not return a result set

  6. 6

    Spring Data JPA - The statement did not return a result set

  7. 7

    execute stored procedure in entity Framework Core without expecting map to dbset

  8. 8

    Spring Data JPA @OneToMany

  9. 9

    Retrieve output from stored procedure result in R with odbc/DBI

  10. 10

    T-SQL stored procedure to get data from any table on the server for CSV export (SQL Server 2016)

  11. 11

    try to render data from 2 different source using .map() function

  12. 12

    Spring Data JPA Closed Connection

  13. 13

    Spring Data JPA Lazy Loading

  14. 14

    Spring Jpa Data、Pageable、Pagerequest

  15. 15

    Spring Jpa Data、Pageable、Pagerequest

  16. 16

    Spring Data JPA - Specifications and Querydsl

  17. 17

    Spring Data JPA + JpaSpecificationExecutor + NamedEntityGraph

  18. 18

    Data JPA Test with reactive spring

  19. 19

    Spring data jpa limit not working

  20. 20

    Spring Data JPA + Hibernate + PostgreSQL

  21. 21

    Netezza - How to return multiple values from a stored procedure without REFTABLE

  22. 22

    How to return multiple values from stored procedure within tsql query

  23. 23

    SQL Server : 2 Stored Procedure 1 for XML & one for data

  24. 24

    Pull a set of data through stored procedure and send an email

  25. 25

    Search stored procedure returns cut off column data

  26. 26

    Is possible map a single entity with multiple tables using JPA?

  27. 27

    Spring + hibernate versus Spring Data JPA: Are they different?

  28. 28

    Spring + hibernate versus Spring Data JPA: Are they different?

  29. 29

    Spring + hibernate versus Spring Data JPA: Are they different?

ホットタグ

アーカイブ