2017-12-22 7 views
2

私がしたいのは、毎月の合計数を取得することです。だから私は これらのクエリで来た。 @Queryを使用してSpringブートJPA リポジトリのドキュメントに従った。しかし、私のアプリを実行すると、アプリケーション 起動に失敗します。私の質問が正しいかどうかはわかりません。ここでここでのクエリの使い方は? - JPAリポジトリのスプリングブート

は私の@queryは「私は達成したい何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; 

} 

がステータスで出荷数を取得することです

@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); 

です今年度に基づいて各月に完了しました。

+1

まず、あなたが取得している例外が何ですか。それを置く。 第2に、問題の詳細を説明する必要があります。 Shipmentクラスはどのように見えますか? – pvpkiran

+1

http://idownvotedbecau.se/noexceptiondetails/ http://idownvotedbecau.se/nomcve/ – Synch

+0

クエリが返されないintまたはlongを返す出荷 –

答えて

2

データベースからカスタム情報を取得する必要がある場合(たとえば、月番号と関連レコードの数)、クエリメソッドからエンティティ全体を返すことはできません。 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); 
} 

結果:あなたが、その場合には、あなたのHQLクエリでselect *操作を行うと

{month=1, count=1} 
{month=2, count=1} 
{month=3, count=1} 
{month=12, count=2} 
1

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); 
関連する問題