Getting error Could not locate appropriate constructor on class

Basit

I am trying to map native SQL result to my POJO. Here is the configuration. I am using spring.

<bean id="ls360Emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
    <property name="dataSource" ref="ls360DataSource" />
    <property name="jpaVendorAdapter" ref="vendorAdaptor" />         
    <property name="packagesToScan" value="abc.xyz"/>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.max_fetch_depth">3</prop>
            <prop key="hibernate.jdbc.fetch_size">50</prop>
            <prop key="hibernate.jdbc.batch_size">10</prop>
            <prop key="hibernate.show_sql">true</prop>              
        </props>        
    </property>
</bean> 

Here is my Class

@SqlResultSetMapping(
    name="courseCompletionMapping", 
    classes = {
        @ConstructorResult(targetClass = CourseCompletion.class,
            columns={
                @ColumnResult(name = "StoreId", type = String.class),
                @ColumnResult(name = "ProductId", type = String.class),
                @ColumnResult(name = "UserName", type = String.class),
                @ColumnResult(name = "Score", type = Integer.class),
                @ColumnResult(name = "CompletionDate", type = Date.class)
             }
        )
    }
) 
@Entity
public class CourseCompletion {
    private String storeId;

    @Id
    private String productId;
    private String userName;
    private int score;
    private Date completionDate;

    public CourseCompletion() {
    }

    public CourseCompletion(String storeId, String productId, String userName, int score, Date completionDate) {
        this.storeId = storeId;
        this.productId = productId;
        this.userName = userName;
        this.score = score;
        this.completionDate = completionDate;
    }

    // getters and setters

Here how i am calling it

    Properties coursePropertiesFile = SpringUtil.loadPropertiesFileFromClassPath("course.properties");
    String queryString = coursePropertiesFile.getProperty("course.completion.sql");

    long distributorId = 1;
    String fromDate = "2009-09-22 00:00:00";
    String toDate = "2014-04-11 23:59:59";

     Query query = em.createNativeQuery(queryString, "courseCompletionMapping");

     //Query query = em.createNamedQuery("findAllEmployeeDetails");
     query.setParameter("distributorId", distributorId);
     query.setParameter("fromDate", fromDate);
     query.setParameter("toDate", toDate);

     @SuppressWarnings("unchecked")
     List<CourseCompletion> courseCompletionList = query.getResultList();

But when it comes to line

List<CourseCompletion> courseCompletionList = query.getResultList();

I get an error that

Could not locate appropriate constructor on class : mypackage.CourseCompletion

Here is the query that i am trying

select d.DISTRIBUTORCODE AS StoreId, u.USERGUID AS ProductId, u.UserName,
    lcs.HIGHESTPOSTTESTSCORE AS Score, lcs.CompletionDate 
from VU360User u 
inner join learner l on u.ID = l.VU360USER_ID 
inner join LEARNERENROLLMENT le on le.LEARNER_ID = l.ID 
inner join LEARNERCOURSESTATISTICS lcs on lcs.LEARNERENROLLMENT_ID = le.ID 
inner join customer c on c.ID = l.CUSTOMER_ID 
inner join DISTRIBUTOR d on d.ID = c.DISTRIBUTOR_ID 
where d.ID = :distributorId 
and lcs.COMPLETIONDATE is not null 
and (lcs.COMPLETIONDATE between :fromDate and :toDate) 
and lcs.COMPLETED = 1

Why i am getting this error ?

Thanks

Zmicier Zaleznicenka

This exception happens because JPA doesn't change column types returned from the database for native queries. Because of this, you have type mismatch. I'm not sure about which column causes this problem in your case (this depends on DBMS you use), but I would suspect you have BigInteger in the result set instead of Integer for score column. To be 100% sure, add a breakpoint to ConstructorResultColumnProcessor.resolveConstructor(Class targetClass, List<Type> types) and investigate. After you find a mismatch, change field type in your mapping class.

Another solution will be not to use @SqlResultSetMapping at all. As your CourseCompletion class is a managed entity, you should be able to map native query to it directly. See this question for more information.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

hibernate: Unable to locate appropriate constructor on class - HQL

From Dev

Criteria API Unable to locate appropriate constructor on class

From Dev

JPA - Unable to locate appropriate constructor on class

From Dev

org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class

From Dev

General error: could not call class constructor'

From Dev

org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor

From Dev

Meteor run server getting error: "Could not locate the bindings file. [...]bcrypt[...]"

From Dev

rails command does not work. getting error "Could not locate Gemfile or .bundle/ directory"

From Dev

I face this error java.lang.IllegalArgumentException: Could not locate call adapter for class java.lang.Object

From Java

(Retrofit) Could not locate converter for class crashing app

From Dev

no appropriate default constructor available error with initializer list

From Dev

Play framework - securesocial - Could not find an appropriate constructor to instantiate plugin

From Dev

Play framework - securesocial - Could not find an appropriate constructor to instantiate plugin

From Dev

`Error! Could not locate dkms.conf file`

From Dev

Error: Could not locate the bindings file. Tried:

From Dev

Capistrano could not locate Gemfile error on application deployment

From Dev

Unexpected Error : `<<' unmatched. Could not locate the mistake

From Dev

Unexpected Error : `<<' unmatched. Could not locate the mistake

From Dev

Ctor-initializer for a class doesn't call to the appropriate constructor

From Dev

C++ templated class "no appropriate default constructor available"

From Dev

Class could not be found error

From Dev

OS X productsign error: Could not find appropriate signing identity

From Dev

Typescript error "class is not a constructor"

From Dev

Error on constructor in derived class

From Dev

Could not locate clojure/tools/logging__init.class

From Dev

error C2512: no appropriate default constructor available (not classes)

From Dev

C++ Error C2512: no appropriate default constructor available

From Dev

Keep getting NullPointerException error on the constructor

From Dev

Getting error unbound variable or constructor

Related Related

  1. 1

    hibernate: Unable to locate appropriate constructor on class - HQL

  2. 2

    Criteria API Unable to locate appropriate constructor on class

  3. 3

    JPA - Unable to locate appropriate constructor on class

  4. 4

    org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class

  5. 5

    General error: could not call class constructor'

  6. 6

    org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor

  7. 7

    Meteor run server getting error: "Could not locate the bindings file. [...]bcrypt[...]"

  8. 8

    rails command does not work. getting error "Could not locate Gemfile or .bundle/ directory"

  9. 9

    I face this error java.lang.IllegalArgumentException: Could not locate call adapter for class java.lang.Object

  10. 10

    (Retrofit) Could not locate converter for class crashing app

  11. 11

    no appropriate default constructor available error with initializer list

  12. 12

    Play framework - securesocial - Could not find an appropriate constructor to instantiate plugin

  13. 13

    Play framework - securesocial - Could not find an appropriate constructor to instantiate plugin

  14. 14

    `Error! Could not locate dkms.conf file`

  15. 15

    Error: Could not locate the bindings file. Tried:

  16. 16

    Capistrano could not locate Gemfile error on application deployment

  17. 17

    Unexpected Error : `<<' unmatched. Could not locate the mistake

  18. 18

    Unexpected Error : `<<' unmatched. Could not locate the mistake

  19. 19

    Ctor-initializer for a class doesn't call to the appropriate constructor

  20. 20

    C++ templated class "no appropriate default constructor available"

  21. 21

    Class could not be found error

  22. 22

    OS X productsign error: Could not find appropriate signing identity

  23. 23

    Typescript error "class is not a constructor"

  24. 24

    Error on constructor in derived class

  25. 25

    Could not locate clojure/tools/logging__init.class

  26. 26

    error C2512: no appropriate default constructor available (not classes)

  27. 27

    C++ Error C2512: no appropriate default constructor available

  28. 28

    Keep getting NullPointerException error on the constructor

  29. 29

    Getting error unbound variable or constructor

HotTag

Archive