在非实体类上使用@NamedNativeQueries选择连接

时间:2018-01-28 11:14:24

标签: jpa spring-boot

我想从复杂连接选择中检索数据仅用于报告建议。我决定在没有@Entity注释的类中使用@NamedNativeQueries,如下所示

@NamedNativeQueries({
        @NamedNativeQuery(name = "detailInformationForInvoicePurpose",
                query = "SELECT DISTINCT b.ShipName, \n" +
                        "    b.ShipAddress, \n" +
                        "    b.ShipCity, \n" +
                        "    b.ShipRegion, \n" +
                        "    b.ShipPostalCode, \n" +
                        "    b.ShipCountry, \n" +
                        "    b.CustomerID, \n" +
                        "    c.CompanyName, \n" +
                        "    c.Address, \n" +
                        "    c.City, \n" +
                        "    c.Region, \n" +
                        "    c.PostalCode, \n" +
                        "    c.Country, \n" +
                        "    concat(d.FirstName,  ' ', d.LastName) AS Salesperson, \n" +
                        "    b.OrderID, \n" +
                        "    b.OrderDate, \n" +
                        "    b.RequiredDate, \n" +
                        "    b.ShippedDate, \n" +
                        "    a.CompanyName, \n" +
                        "    e.ProductID, \n" +
                        "    f.ProductName, \n" +
                        "    e.UnitPrice, \n" +
                        "    e.Quantity, \n" +
                        "    e.Discount,\n" +
                        "    e.UnitPrice * e.Quantity * (1 - e.Discount) AS ExtendedPrice,\n" +
                        "    b.Freight\n" +
                        "FROM Shippers a \n" +
                        "INNER JOIN Orders b ON a.ShipperID = b.ShipVia \n" +
                        "INNER JOIN Customers c ON c.CustomerID = b.CustomerID\n" +
                        "INNER JOIN Employees d ON d.EmployeeID = b.EmployeeID\n" +
                        "INNER JOIN Order_Details e ON b.OrderID = e.OrderID\n" +
                        "INNER JOIN Products f ON f.ProductID = e.ProductID\n" +
                        "ORDER BY b.ShipName",
                resultClass = JpaReport.class)
})
public class JpaReport {
    private String shipname;
    private String shipaddress;
    private String shipcity;
    private String shipregion;
    private String shippostalcode;
    private String shipcountry;
    private String customerid;
    private String companyname;
    private String address;
    private String city;
    private String region;
    private String postalcode;
    private String country;
    private String lastname;
    private String firstname;
    private Long orderid;
    private java.sql.Timestamp orderdate;
    private java.sql.Timestamp requireddate;
    private java.sql.Timestamp shippeddate;

    public JpaReport(String shipname, String shipaddress, String shipcity, String shipregion, String shippostalcode, String shipcountry, String customerid, String companyname, String address, String city, String region, String postalcode, String country, String lastname, String firstname, Long orderid, Timestamp orderdate, Timestamp requireddate, Timestamp shippeddate) {
        this.shipname = shipname;
        this.shipaddress = shipaddress;
        this.shipcity = shipcity;
        this.shipregion = shipregion;
        this.shippostalcode = shippostalcode;
        this.shipcountry = shipcountry;
        this.customerid = customerid;
        this.companyname = companyname;
        this.address = address;
        this.city = city;
        this.region = region;
        this.postalcode = postalcode;
        this.country = country;
        this.lastname = lastname;
        this.firstname = firstname;
        this.orderid = orderid;
        this.orderdate = orderdate;
        this.requireddate = requireddate;
        this.shippeddate = shippeddate;
    }

当我尝试从JpaReport类中检索数据列表时,我有信息请查看截图

@Repository
public class JpaReportDaoImpl {

    @PersistenceContext
    private EntityManager em;

    public List<JpaReport> detailInformationForInvoicePurpose(){
        List<JpaReport> jpaReport = em.createNamedQuery("detailInformationForInvoicePurpose", JpaReport.class).getResultList();
        return jpaReport;
    }

}

enter image description here

我有一个问题:

  • 我做错了什么?
  • 这是一种仅为其检索数据的好方法 报告?

我也尝试按如下方式提取数据

 public List<JpaReport> detailInformationForInvoicePurpose(){
        List<JpaReport> jpaReport = em.createNativeQuery("SELECT DISTINCT b.ShipName, \n" +
                "    b.ShipAddress, \n" +
                        "    b.ShipCity, \n" +
                        "    b.ShipRegion, \n" +
                        "    b.ShipPostalCode, \n" +
                        "    b.ShipCountry, \n" +
                        "    b.CustomerID, \n" +
                        "    c.CompanyName as customersCompasnyName, \n" +
                        "    c.Address, \n" +
                        "    c.City, \n" +
                        "    c.Region, \n" +
                        "    c.PostalCode, \n" +
                        "    c.Country, \n" +
                        "    concat(d.FirstName,  ' ', d.LastName) AS Salesperson, \n" +
                        "    b.OrderID, \n" +
                        "    b.OrderDate, \n" +
                        "    b.RequiredDate, \n" +
                        "    b.ShippedDate, \n" +
                        "    a.CompanyName as shipersCompanyName, \n" +
                        "    e.ProductID, \n" +
                        "    f.ProductName, \n" +
                        "    e.UnitPrice, \n" +
                        "    e.Quantity, \n" +
                        "    e.Discount,\n" +
                        "    e.UnitPrice * e.Quantity * (1 - e.Discount) AS ExtendedPrice,\n" +
                        "    b.Freight\n" +
                        "FROM Shippers a \n" +
                        "INNER JOIN Orders b ON a.ShipperID = b.ShipVia \n" +
                        "INNER JOIN Customers c ON c.CustomerID = b.CustomerID\n" +
                        "INNER JOIN Employees d ON d.EmployeeID = b.EmployeeID\n" +
                        "INNER JOIN Order_Details e ON b.OrderID = e.OrderID\n" +
                        "INNER JOIN Products f ON f.ProductID = e.ProductID\n" +
                        "ORDER BY b.ShipName").getResultList();
        return jpaReport;
    }

但是当我想按照List<JpaReport>进行迭代时,我得到一个错误

List<JpaReport> jpaReports = jpaReportDaoImpl.detailInformationForInvoicePurpose();
        for (JpaReport jpaReport : jpaReports) {
            System.out.println(jpaReport);
        }

引起:java.lang.ClassCastException:[Ljava.lang.Object;不能转换为###################。model.JpaReport

1 个答案:

答案 0 :(得分:1)

因为它不是托管实体类,所以根本不处理查询注释,导致查询丢失。将查询注释移动到托管(@Entity)类。