如何使用@SqlResultSetMapping 获取复杂本机查询的结果集?

问题描述 投票:0回答:0

我在我的项目中使用 spring boot 和 spring data JPA。我需要获取复杂本机查询的结果集,但我不确定如果它们不是实体,如何获取查询和结果集。所以我没有将结果集发送到 DTO 或映射的实体类,因为我只想获取查询的结果集并能够在 API 上使用它。

我在我的存储库层中使用属性

resultSetMapping ="ProductOrderInvoiceMapping"
,但它突出显示为
cannot find simbol
。我的实施似乎不起作用,所以我想知道什么是最好的方法。

这是我的代码:

ProductOrderInvoiceDTO,它不是真正的 DTO,因为它不映射任何表或作为实体。

package co.com.ssc.txdb.domain;

import javax.persistence.ColumnResult;
import javax.persistence.ConstructorResult;
import javax.persistence.SqlResultSetMapping;
import lombok.Data;

@Data
@SqlResultSetMapping(
    name = "ProductOrderInvoiceMapping",
    classes = {
        @ConstructorResult(
            targetClass = ProductOrderInvoiceDTO.class,
            columns = {
                @ColumnResult(name = "userData", type = String.class), //userData
                @ColumnResult(name = "contactData", type = String.class), // contactData
                @ColumnResult(name = "customerGroupName", type = String.class), // CustomerGroupName
                @ColumnResult(name = "productOrderData", type = String.class), // InvoiceData
                @ColumnResult(name = "invoiceData", type = String.class), // ProductName
                @ColumnResult(name = "productName", type = String.class), //
                @ColumnResult(name = "productOrderStartDate", type = String.class), // 
                @ColumnResult(name = "lastInvoicePaymentDate", type = String.class), // 
                @ColumnResult(name = "invoiceDetailsData", type = String.class),
                @ColumnResult(name = "firstInvoiceCreatingDate", type = String.class)
            }
        )
    }
)

public class ProductOrderInvoiceDTO {

    private String userData; // SELECT joining the "auth_user_contact" and "auth_user". Used to retrieve seller info for the invoice
    private String contactData; // SELECT json_data from "contact" table
    private String customerGroupName; // The select is selecting the "name" column from the "customergroup" table
    private String productOrderData; // SELECT "json_data" from the "productOrder" table
    private String invoiceData; // SELECT "json_data" from the "invoice" table
    private String productName; // SELECT minimun value of the "name" column from the "productOrder". Used to retrieve the start date of the product order
    private String productOrderStartDate; // SELECT the column "received_start_date" from "productOrder" table. Retrieve the start date of the product order
    private String lastInvoicePaymentDate; // SELECT "transaction_date" column from the table "invoice_payment". Used to retrieve the transaction date of the invoice payment
    private String invoiceDetailsData; // SELECT "json_data" from "invoice_details" table
    private String firstInvoiceCreatingDate; // SELECT "creating_date" column from the "invoice" table. Retrieved to create date of the invoice

    public ProductOrderInvoiceDTO(String userData, String contactData, String customerGroupName, String productOrderData, String invoiceData, String productName, String productOrderStartDate, String lastInvoicePaymentDate, String invoiceDetailsData, String firstInvoiceCreatingDate) {
        this.userData = userData;
        this.contactData = contactData;
        this.customerGroupName = customerGroupName;
        this.productOrderData = productOrderData;
        this.invoiceData = invoiceData;
        this.productName = productName;
        this.productOrderStartDate = productOrderStartDate;
        this.lastInvoicePaymentDate = lastInvoicePaymentDate;
        this.invoiceDetailsData = invoiceDetailsData;
        this.firstInvoiceCreatingDate = firstInvoiceCreatingDate;
    }
}

资料库

package co.com.ssc.txdb.repository;

import co.com.ssc.txdb.domain.ProductOrderInvoiceDTO;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import javax.persistence.SqlResultSetMapping;

@Repository
public interface ProductOrderInvoiceRepository extends JpaRepository<ProductOrderInvoiceDTO, Long>{

    @Query(value = "select " 
            + "(select au.json_data from auth_user_contact auc join auth_user au "
            + "on (auc.user_id = au.id) where auc.contact_id = c.seq_id and "
            + "(auc.seller_profile_id = 1 or auc.seller_profile_id is null) limit 1), " 
            + "c.json_data, "
            + "cg.name, " 
            + "po.json_data, " 
            + "i.json_data, " 
            + "MIN(p.name), "
            + "(select received_start_date from productorder where product_id is not null and contact_id = c.seq_id order by seq_id limit 1), " 
            + "(select sip.transaction_date from invoice_payment sip where sip.contact_id = c.seq_id order by sip.seq_id desc limit 1), "
            + "id.json_data, "
            + "(select creating_date from invoice where contact_id = c.seq_id and type_invoice=1 order by seq_id asc limit 1) "
            + "from "
            + "invoice_details id join invoice i on (id.invoice_id = i.seq_id) " 
            + "join contact c on (i.contact_id = c.seq_id) "
            + "join customergroup cg on (c.customergroup_id = cg.seq_id) "
            + "left join productorder po on (i.productorder_id = po.seq_id) " 
            + "left join productorder_detail pd on (po.seq_id = pd.productorder_id) " 
            + "left join product p on (id.product_id = p.seq_id) " 
            + "where " 
            + "i.type_invoice = 1 " 
            + "group by " 
            + "id.json_data,i.seq_id,i.json_data, " 
            + "c.json_data,c.seq_id,cg.name, "
            + "po.json_data " 
            + "order by c.seq_id, c.json_data, po.json_data, i.seq_id ",
            nativeQuery = true, resultSetMapping ="ProductOrderInvoiceMapping")
    List<ProductOrderInvoiceDTO> getInvoiceDetails();
}

提前谢谢你。

java spring spring-boot spring-data-jpa hibernate-mapping
© www.soinside.com 2019 - 2024. All rights reserved.