gpt4 book ai didi

spring-data-jpa - 出现错误无法在类上找到适当的构造函数

转载 作者:行者123 更新时间:2023-12-03 01:11:44 25 4
gpt4 key购买 nike

我正在尝试将 native SQL 结果映射到我的 POJO。这是配置。我正在使用 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>

这是我的类(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

这是我的称呼

    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

为什么我会收到此错误?

谢谢

最佳答案

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

另一个解决方案是不使用 @SqlResultSetMapping根本不。作为您的CourseCompletion类是一个托管实体,您应该能够将 native 查询直接映射到它。请参阅this question了解更多信息。

关于spring-data-jpa - 出现错误无法在类上找到适当的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24160817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com