调用init方法失败;嵌套异常是java.lang.IllegalArgumentException:不是托管类型:

时间:2018-11-13 14:52:21

标签: java spring spring-boot spring-data-jpa spring-data

下面是我的普通POJO对象,它具有所有映射。请注意,它不是实体对象。

import javax.persistence.ColumnResult;
import javax.persistence.ConstructorResult;
import javax.persistence.NamedNativeQuery;
import javax.persistence.SqlResultSetMapping;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@AllArgsConstructor
@SqlResultSetMapping(name = "refundReportMapping", classes = { @ConstructorResult(targetClass = RefundReport.class, columns = {
        @ColumnResult(name = "customer_name"),
        @ColumnResult(name = "hashed_email"), @ColumnResult(name = "address"),
        @ColumnResult(name = "partner_order"),
        @ColumnResult(name = "refund_amount") }) })
@NamedNativeQuery(name = "findRefundReportByStatusNamesParamsNative", query = "SELECT concat(bd.first_name,' ',bd.last_name) as 'customer_name',bd.email_address AS 'hashed_email',concat(bd.address1,' ',bd.address2) AS 'address',"
        + o.reservation_id AS 'partner_order',"
        + "(oa.principal_amount+oa.shipping_amount+oa.tax_amount) AS 'refund_amount'"
        + "FROM orders o, order_adjustments oa WHERE oa.status = :status"
        + "and o.buyer_detail_id=bd.buyer_detail_id and o.reservation_id=oa.order_id", resultClass = RefundReport.class, resultSetMapping = "refundReportMapping")
public class RefundReport {

    private String customerName, email, address, 
            partnerOrder;
    private BigDecimal partilaRefundAount;

}
public interface RefundReportRepository extends
        Repository<RefundReport, String> {

    List<RefundReport> findRefundReportByStatus(
            @Param("status") String status);

}

1 个答案:

答案 0 :(得分:0)

此代码存在多个问题。

  1. Repository需要两个类型参数。它是存储库的实体类型和id类型的类型。如果没有说明这些类型,Spring Data会为两种类型的Object确定Object,因为RefundReport不是托管实体。另外,如果您打算使用using (SqlConnection sqlConn = new SqlConnection("myConnectionString")) { sqlConn.Open(); ... } ,则它必须是JPA管理的实体。

  2. 正如@ HaseebR7所指出的那样,命名查询等的注释也必须在实体上。

由于似乎您只想执行查询,因此可以使用任何实体,但是它必须是具有id的适当实体。