What I want to do is to get the total count in each month. So I have come up with these queries. I followed the documentation on Spring Boot JPA repository using @Query. But when I run my app, it goes Application Startup Failed. I dont know if my query is right.
Here is my @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;
}
What I want to achieve is to get the count of shipments with the status 'Completed' in each month based on the current year.
最佳答案
如果您需要从数据库获取自定义信息,例如月份数和相关记录数,则无法从查询方法返回整个实体。您必须使用 projection 。例如:
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}
关于java - 如何使用 at 查询 at 这个? - JPA存储库 Spring 启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47943896/