I'm using spring boot and spring data JPA in my project. I need to get the resultset of a complex native query, but I'm unsure of how to get the query and resultSet if they are not entities. So I'm not sending the resultset to a DTO or mapped entity class as I just want to get the resulset of the query and be able to use it on an API.
I'm using the attribute resultSetMapping ="ProductOrderInvoiceMapping"
in my repository layer, but it is highlighting it as cannot find simbol
. My implementation seems not to be working so I want to know what is the best way to do this.
This is my code:
ProductOrderInvoiceDTO, it is not a real DTO as it is not mapping any table or be an Entity.
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;
}
}
Repository
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();
}
Thank you in advance.