JPA本机查询结果集映射到具有子类的实体类

时间:2019-05-16 16:24:35

标签: java spring-boot jpa spring-data-jpa hibernate-native-query

运行代码时,我在jpa自定义方法中使用entityManager.createNativeQuery(somequery)编写联接查询,但出现以下错误:

  

com.ibm.db2.jcc.am.SqlException:[jcc] [10150] [10300] [4.12.56]无效>参数:未知的列名exc_seq_nbr。 ERRORCODE = -4460,SQLSTATE =空

     

我正在使用IBM DB2服务器和Spring Boot

exceptionTenderPK(实体类中的对象)未正确映射,这就是为什么有人获取无效列的原因,请告诉我如何映射exceptionTenderPK对象类

  

注意:在这种情况下,我不能使用@OneToMany,因为表是不相关的

@Entity
@Table(name = "EPT_TNDR_EXC")


@SqlResultSetMapping(
        name = "exceptionTenderMapping",
        entities = {
            @EntityResult(
                    entityClass = ExceptionTender.class,
                    fields = {
                    @FieldResult(name =  "exceptionTenderPK.salesDate", column = "SLS_DTE")
                    ,@FieldResult(name = "exceptionTenderPK.storeNumber", column = "STR_NBR")
                    ,@FieldResult(name = "exceptionTenderPK.registerNumber", column = "RGST_ID")
                    ,@FieldResult(name = "exceptionTenderPK.transactionNumber", column = "TRN_NBR")
                    ,@FieldResult(name = "exceptionTenderPK.startTime", column = "STRT_TM")
                    ,@FieldResult(name = "exceptionTenderPK.exceptionSequenceNumber", column = "EXC_SEQ_NBR")
                 ,@FieldResult(name = "tenderAmount", column = "TNDR_AMT")
                ,@FieldResult(name = "tndrId", column = "TNDR_ID")
                ,@FieldResult(name = "reason", column = "EXC_RSN_CDE")
                ,@FieldResult(name = "completionIndicator", column = "CPL_IND")
                ,@FieldResult(name = "userId", column = "LAST_UPD_USR_ID")
                ,@FieldResult(name = "lastUpdatedTmst", column = "LAST_UPD_TMST")
                ,@FieldResult(name = "excTrnSrcCde", column = "EXC_TRN_SRC_CDE")
                ,@FieldResult(name = "excTrnStatCde", column = "EXC_TRN_STAT_CDE")
            })
        })


@Data
public class ExceptionTender {

    @EmbeddedId
    @JsonProperty(value = "exceptionTenderPK")
    private ExceptionTenderPK exceptionTenderPK;


    @Column(name = "TNDR_AMT")
    @JsonProperty(value = "TNDR_AMT")
    private BigDecimal tenderAmount;

    @Column(name = "TNDR_ID")
    @JsonProperty(value = "TNDR_ID")
    private String tndrId;

    @Column(name = "EXC_RSN_CDE")
    @JsonProperty(value = "EXC_RSN_CDE")
    private String reason;

    @Column(name = "CPL_IND")
    @JsonProperty(value = "CPL_IND")
    private String completionIndicator;

    @Column(name = "LAST_UPD_USR_ID")
    @JsonProperty(value = "LAST_UPD_USR_ID")
    private String userId;

    @Column(name = "LAST_UPD_TMST")
    @JsonProperty(value = "LAST_UPD_TMST")
    private Date lastUpdatedTmst;

    @Column(name = "EXC_TRN_SRC_CDE")
    @JsonProperty(value = "EXC_TRN_SRC_CDE")
    private String excTrnSrcCde;

    @Column(name = "EXC_TRN_STAT_CDE")
    @JsonProperty(value = "EXC_TRN_STAT_CDE")
    private String excTrnStatCde;

    @Transient
    @QueryType(PropertyType.STRING)
    private String statusCode;

}

@Transactional
public interface JoinQueryRepository extends JpaRepository<ExceptionTender, Long>, JoinQueryRepositoryCustom{

}

public interface JoinQueryRepositoryCustom {

    List<ExceptionTender> getGRDetails(ExceptionTender et,Date reportDate);

}

public class JoinQueryRepositoryImpl implements JoinQueryRepositoryCustom {

    @PersistenceContext
    EntityManager entityManager;

    @SuppressWarnings("all")
    @Override
    public List<ExceptionTender> getGRDetails(ExceptionTender et,Date reportDate) {
String queryStr = "select et.SLS_DTE from db272dba.ept_tndr_exc et"
                + " join db272dba.EPT_TNDR_EXC_RPT_DTE etr on et.SLS_DTE = etr.SLS_DTE where et.STR_NBR =? ";

        List<ExceptionTender> grDetailsList = null;

        Query query =   entityManager.
                createNativeQuery(queryStr,"exceptionTenderMapping")
                .setParameter(1, 222);
        grDetailsList = query.getResultList();

        return grDetailsList;

    }

}

2 个答案:

答案 0 :(得分:0)

该错误表明没有列exc_seq_nbr,您已在EntityResult映射中使用它。

在查询中,您仅返回et.SLS_DTE,您必须返回结果集映射中的所有列。

答案 1 :(得分:0)

大家好,因为我没有任何解决方案,因此我使用下面的解决方案对我有用,并且在没有sql结果集映射的情况下删除下面的代码@SqlResultSetMapping是有效的

Query q = em.createNativeQuery(queryStr);
List<Object[]> resultList = q.getResultList();

for (Object[] result : resultList) {
   entityObj.setReason(result[0].toString);
//rest attribute will convert from result[1].toString to corresponding
// data type and set to entity object
}