spring - 嵌套异常是 org.hibernate.HibernateException : Could not instantiate resultclass

标签 spring spring-boot

出了什么问题?

public List < ReportDTO> listProductAll() {
    String sql 
            = "select "
            + "ip.product_name as productName, "
            + "ip.base_price as basePrice, "
            + "iu.username as username "
            + "from tb_buy a  "
            + "left join in_product ip on a.id_product = ip.product_id "
            + "left join im_users iu on a.id_user = iu.user_id ";
    Query q = identifyServer.getCurrentSession().createSQLQuery(sql)
            .addScalar("productName")
            .addScalar("basePrice")
            .addScalar("username")
            .setResultTransformer(Transformers.aliasToBean(ReportDTO.class));
    return q.list();
}

public class ReportDTO {

    private String productName;
    private Double basePrice;
    private String username;

    public ReportDTO(String productName, Double basePrice, String username) {
        this.productName = productName;
        this.basePrice = basePrice;
        this.username = username;
    }
// getter setter

org.springframework.orm.jpa.JpaSystemException: Could not instantiate resultclass: ReportDTO; nested exception is org.hibernate.HibernateException: Could not instantiate resultclass: ReportDTO

解决 公共(public) ReportDTO() {}

最佳答案

Hibernate 要求所有实体都有一个默认的无参数构造函数。
如果您的实体类没有它,或者它不是公共(public)的,您将收到此异常。

关于spring - 嵌套异常是 org.hibernate.HibernateException : Could not instantiate resultclass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46743950/

相关文章:

java - Spring security PreAuthentication Filter 要求 AuthenticationEntryPoint

java - 无法使用 Maven 构建 Spring 4 项目

java - @Transactional 服务方法回滚 hibernate 更改

spring - 如何在后期初始化中保留不可为空的属性

java - Spring Boot Hibernate 将枚举集映射到 boolean 列

spring - Spring JDBC 是否提供任何针对 SQL 注入(inject)攻击的保护?

java - Spring应用程序中如何在html页面中添加图片

java - Spring 批处理 : How to implement a many-to-one ItemProcessor

spring - 运行 bootRun 并使用不同的 application.properties 文件

java - com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException : Cannot delete or update a parent row: a foreign key constraint fails