[英]How to use at Query at this? - JPA repository spring boot
我要做的是獲取每個月的總數。 因此,我提出了這些查詢。 我使用@Query關注了Spring Boot JPA存儲庫中的文檔。 但是,當我運行我的應用程序時,應用程序啟動失敗。 我不知道我的查詢是否正確。
這是我的@Query
@Query("SELECT MONTH(date_completed) FROM shipment WHERE YEAR(date_created)=?1 GROUP BY MONTH(date_completed)=?1")
public List<Shipment> findByDate_completed(String date);
@Query("SELECT COUNT(*) FROM shipment WHERE YEAR(date_created)=?1 GROUP BY MONTH(date_completed)=?1")
public List<Shipment> countByDate_completed(String date);
Shipment.java
package com.pahrsek.smartfleet.model;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* Delivery of goods
* @author JRDomingo
*
*/
@Entity
@Table(name="shipment")
public class Shipment {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long id;
@Column(name="booking_number",unique=true)
public String bookingNumber;
@Column(name="wb_number")
public String wbNumber;
@ManyToOne
@JoinColumn(name="vehicle_id", referencedColumnName="id")
public Vehicle vehicle;
@ManyToOne
@JoinColumn(name="customer_id", referencedColumnName="id")
public Customer customer;
public String origin;
public String depot;
@ManyToOne
@JoinColumn(name="vendor_id", referencedColumnName="id")
public Vendor vendor;
public String type;
@Column(name="commodity_type")
public String commodityType;
@Column(name="truck_type")
public String truckType;
@Enumerated(EnumType.STRING)
public Status status;
@Column(name="delivery_date")
public Date deliveryDate;
@Column(name="fuel_po")
public String fuelPo;
@Column(name="client_ref_no")
public String clientReferenceNumber;
public String remarks;
@ManyToOne
@JoinColumn(name="driver_id",referencedColumnName="id")
public Personnel driver;
@ManyToOne
@JoinColumn(name="helper1_id",referencedColumnName="id")
public Personnel helper1;
@ManyToOne
@JoinColumn(name="helper2_id",referencedColumnName="id")
public Personnel helper2;
public enum Status{
New, Dispatched, Delivered, Completed, Cancelled
}
/******
* ACTUAL DATES IMPLEMENTED
******/
@Column(name="date_created")
public Date dateCreated;
@Column(name="date_dispatched")
public Date dateDispatched;
@Column(name="date_completed")
public Date dateCompleted;
@Column(name="date_cancelled")
public Date dateCancelled;
@Column(name="date_received")
public Date dateReceived;
@Column(name="farthest_destination")
public String farthestDestination;
@Column(name="client_rate")
public Double clientRate;
@Column(name="is_sub_con")
public boolean isSubCon;
@Column(name="sub_con_rate")
public Double subConRate;
@Column(name="fuel")
public Double fuel;
@Column(name="fuel_amount")
public Double fuelAmount;
@Column(name="route_code")
public String routeCode;
@ManyToOne
@JsonIgnore
@JoinColumn(name="dispatched_odometer_id",referencedColumnName="id")
public RegularOdometerUsage dispatchedOdometer;
@ManyToOne
@JsonIgnore
@JoinColumn(name="delivered_odometer_id",referencedColumnName="id")
public RegularOdometerUsage deliveredOdometer;
@ManyToOne
@JsonIgnore
@JoinColumn(name="completed_odometer_id",referencedColumnName="id")
public RegularOdometerUsage completedOdometer;
/**
* index 0 = Driver , index 1 = Helper1, index 2 = Helper2
*/
@JsonIgnore
@OneToMany(mappedBy="shipment",targetEntity=PersonnelRate.class)
public List<PersonnelRate> personnelRates = new ArrayList<PersonnelRate>();
@JsonIgnore
@ManyToOne
@JoinColumn(name="company_id", referencedColumnName="id")
public Company company;
@JsonIgnore
@ManyToOne
@JoinColumn(name="prepared_user_id", referencedColumnName="id")
public User preparedBy;
@JsonIgnore
@ManyToOne
@JoinColumn(name="customer_invoice", referencedColumnName="id")
public CustomerInvoice customerInvoice;
@JsonIgnore
@ManyToOne
@JoinColumn(name="trucker_settlement", referencedColumnName="id")
public TruckerSettlement truckerSettlement;
}
我要實現的是根據當前年份獲得每月狀態為“已完成”的發貨數量。
如果需要從數據庫中獲取自定義信息(例如,月數和相關記錄的數量),則無法從查詢方法返回整個實體。 您必須使用投影 。 例如:
public interface MonthAndCount {
Integer getMonth();
Long getCount();
}
@Query("select month(s.dateCompleted) as month, count(s.id) as count from Shipment s where year(s.dateCompleted) = ?1 group by month(s.dateCompleted)")
List<MonthAndCount> getMonthAndCount(int year);
注意:在此處使用別名( as month
或as count
)很重要,以使框架知道如何填充您的投影。
測試:
@Test
public void getMonthAndCount() {
List<MonthAndCount> monthAndCount = repo.getMonthAndCount(2017);
assertThat(monthAndCount).hasSize(4);
monthAndCount.forEach(System.out::println);
}
結果:
{month=1, count=1}
{month=2, count=1}
{month=3, count=1}
{month=12, count=2}
在這種情況下,如果在hql查詢中執行select *
操作,則hibernate可以將表數據映射到您的實體類,但是如果您通過執行某些操作選擇自定義字段,則需要創建那些映射該數據的投影類。
但是,在這種情況下,如果您不想創建任何類來從查詢中獲取數據,則可以使用Object[]
代替實體類。
所以您的方法將看起來像這樣
@Query("SELECT MONTH(date_completed), COUNT(*) FROM shipment WHERE YEAR(date_created)=?1 GROUP BY MONTH(date_completed)=?1")
public List<Object[]> findByDate_completed(String date);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.