gpt4 book ai didi

java - at Query at 这个怎么用? - JPA 存储库 spring boot

转载 作者:行者123 更新时间:2023-11-30 07:50:14 25 4
gpt4 key购买 nike

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 monthas 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 Query at 这个怎么用? - JPA 存储库 spring boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47943896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com