2014-06-11 49 views
9

我试图将原生SQL结果映射到我的POJO。这是配置。我正在使用春天。获取错误找不到相应的类构造函数

<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> 

这里是我的类

@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 

我在这里怎么叫它

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(); 

但是,当涉及到线路

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

我得到一个错误,

Could not locate appropriate constructor on class : mypackage.CourseCompletion 

这里是我努力

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 

为什么我收到这个错误的查询?

感谢

回答

27

这个异常是因为JPA不会改变列类型从数据库中本地查询返回。正因为如此,你有类型不匹配。我不确定哪个列会导致您的问题出现此问题(这取决于您使用的DBMS),但是我会怀疑在结果集中有BigInteger而不是Integer,因为得分为列。要100%确定,请将断点添加到ConstructorResultColumnProcessor.resolveConstructor(Class targetClass, List<Type> types)并进行调查。在发现不匹配之后,请更改映射类中的字段类型。

另一种解决方案根本不会使用@SqlResultSetMapping。由于您的CourseCompletion类是一个托管实体,因此您应该能够直接将本机查询映射到它。有关更多信息,请参阅this question

+3

谢谢:)。我已经解决了这个问题。我调试程序,然后发现分数列是双重类型。然后,我简单地完成了@ColumnResult(name =“Score”,type = Double.class)'并将分数类型更改为'Double'。 – Basit

+1

非常感谢:)。它有助于调试。在我的情况下问题是由于java.sql.Datetime。 Hibernate将datetime从microsoft sql列转换为java.util.Datetime。在将列类型更改为java.util.Datetime后,它得到解决。 –

+0

非常感谢:)。它有助于调试。在我的情况下问题是由于java.sql.Timestamp。 Hibernate将datetime从microsoft sql列转换为java.util.Date。在将列类型更改为java.util.Date后,它得到解决。 – whoami

相关问题