Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

Spring data projection for native query not mapped to interface

This my repostoriy for retrieve items

@Query(value = "SELECT DISTINCT M.ID as "id", "
        + " M.NAME_PRIMARY_LANG as "name" "
        + " FROM ECOMMERCE_CORE.MERCHANT_ITEMS M , "
        + " ECOMMERCE_CORE.PRODUCT_UNIT_OF_MEASURE P , "
        + " ECOMMERCE_CORE.LOOKUP_TYPES_STATUS S , "
        + " ECOMMERCE_CORE.ITEM_TYPES T , "
        + " ECOMMERCE_CORE.ITEM_PRICE I,"
        + " ECOMMERCE_CORE.MERCHANT_ITEM_BRAND B, "
        + " ECOMMERCE_CORE.MERCHANT_ITEM_CATEGORY C "
        + " WHERE M.ID = P.PRODUCT_ID AND M.ID=I.PRODUCT_ID AND M.ID = B.MERCHANT_ITEM_ID AND  S.ID=M.STATUS_ID AND M.TYPE = T.ID AND M.MERCHANT_ID =?1 AND M.STATUS_ID =?2 "
        + " AND P.BRANCH_ID = ?3 AND I.CHANNEL_ID = ?4 ",
        nativeQuery = true
)
List<ItemModelProjection> findBySupplierIdAndStatusCode(long id, long status, long branchId, long channelId, Pageable pageable);

and this my interface which i need to map the result to it

    @Getter
    @EqualsAndHashCode(of = {"id"})
    public class ItemModelProjection {

    private String id;
    private String name;

    public ItemModelProjection(final String id, final String name) {
        this.id = id;
        this.name = name;
    }}

and the result of this query not mapped to the interface , what is the problem for it ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can solve this issue and achieve the result by using projections by making your DTO an interface with getters for columns returned by the query.

All you need to do is to have interface and contain query domains starting with get.

public interface ItemModelProjection {
    Long getId();
    String getName();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...