2017-01-21 57 views
1

我正在開發一個Web應用程序JavaEE,我使用JPA命名查詢來查詢數據庫。然而,我不知道如何做到這一點,當連接多個分支表,我試圖建立並執行一些查詢以下情況,但它總是返回null如何在JPA NamedQuery中與多個分支連接表

這裏是介紹我的情況的圖像: IMAGE

我在哪裏嘗試選擇從多個表(實體)與多個字段聯接使用@NamedQuery。請注意,我刪除setter和getters從下面的類減少代碼大小。

@MappedSuperclass 
@EntityListeners({Logger.class}) 
public abstract class SuperEntity implements Serializable,HasName{ 

    @Transient 
    private static final long serialVersionUID = -6420076371961889265L; 

    @Id 
    @SequenceGenerator(initialValue = 0, allocationSize = 1, name = "IdGenerator") 
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator = "IdGenerator") 
    private long ID; 

    @Version 
    private long version; 
} 




      @Entity 
      public class Party extends SuperEntity { 

       @Length(min = 1, max = 30, message = "Invalid party name.") 
       private String name; 

       @Enumerated(EnumType.STRING) 
       @NotNull 
       private PartyType type; 

       @Length(min = 1, max = 13, message = "Invalid phone number") 
       private String phone; 

       @Email(message = "Invalid email address.") 
       @NotNull 
       private String email; 

       @NotNull 
       @NotEmpty(message = "Party address is required.") 
       private String address; 

       @Valid 
       @OneToMany(cascade = CascadeType.ALL, mappedBy = "party", fetch = FetchType.EAGER) 
       private List<Vehicle> vehicles = new ArrayList<Vehicle>(); 

     } 

       @Entity 
      public class Vehicle extends SuperEntity { 

       @Valid 
       @Embedded 
       private VehicleAttributes attributes; 

       @ManyToOne(cascade={CascadeType.PERSIST,CascadeType.MERGE}) 
       @JoinColumn(name = "partyID") 
       private Party party; 

       @Valid 
       @OneToMany(cascade = CascadeType.ALL, mappedBy = "vehicle",fetch=FetchType.EAGER) 
       private List<FixRequest> fixRequests = new ArrayList<FixRequest>(); 
      } 


      @NamedQuery(name = FixRequest.GET_NOT_PAID_REQUESTS_BY_PARTY_QUERY, query = "Select new org.master.jpa.UnpaidFixRequestObject(v.attributes.type,v.attributes.model,fr.ID,fr.requestDate,SUM(f.cost),SUM(p.paidAmount)) From Vehicle v,FixRequest fr,Payment p,Fix f,FixJoin fj WHERE v=fr.vehicle AND fr=p.fixRequest AND fr=fj.fixRequest AND fj.fix=f AND v.party.ID=:partyID GROUP BY v.attributes.type,v.attributes.model,fr.ID,fr.requestDate") 
      @Entity 
      public class FixRequest extends SuperEntity implements Serializable { 

      @Transient 
      static final String GET_NOT_PAID_REQUESTS_BY_PARTY_QUERY = "getNotPaidRequestsByParty"; 
      @Transient 
      static final String GET_NOT_PAID_REQUESTS_BY_PARTY_QUERY_PARTY_PARAM = "partyID"; 
       @NotNull 
       @Temporal(TemporalType.TIMESTAMP) 
       private Date requestDate; 

       @NotNull 
       @Temporal(TemporalType.DATE) 
       private Date requestedFixDate; 

       @ManyToOne(cascade={CascadeType.PERSIST,CascadeType.MERGE}) 
       @JoinColumn(name = "vehicleID") 
       private Vehicle vehicle; 

       @OneToMany(cascade = CascadeType.ALL,fetch=FetchType.EAGER,mappedBy="fixRequest") 
       private List<TroubleJoin> request_Troubles = new ArrayList<TroubleJoin>(); 

       @OneToMany(cascade = CascadeType.ALL,fetch=FetchType.EAGER,mappedBy="fixRequest") 
       private List<FixJoin> requset_Fixes = new ArrayList<FixJoin>(); 

       @OneToMany(cascade = CascadeType.ALL,fetch=FetchType.EAGER,mappedBy="fixRequest") 
       private List<Payment> payments = new ArrayList<Payment>(); 

       private String details; 
     } 

     @Entity 
     public class FixJoin extends SuperEntity implements Serializable { 

      public FixJoin() { 
       super(); 
      } 

      @ManyToOne(cascade={CascadeType.PERSIST,CascadeType.MERGE}) 
      @JoinColumn(name = "fixRequestID") 
      private FixRequest fixRequest; 

      @ManyToOne(cascade={CascadeType.PERSIST,CascadeType.MERGE}) 
      @JoinColumn(name = "fixID") 
      private Fix fix; 

      private String details; 
     } 

     @Entity 
     public class Fix extends SuperEntity implements Serializable { 

      @NotEmpty 
      @NotNull 
      private String type; 

      @NotEmpty 
      @NotNull 
      private String detail; 

      @Column(precision = 2) 
      @Min(value = 1) 
      private double cost; 

      @OneToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.LAZY,mappedBy="fix") 
      private List<FixJoin> requset_Fixes = new ArrayList<FixJoin>(); 
     } 

     @Entity 
     public class Payment extends SuperEntity implements Serializable { 

      @Transient 
      private static final long serialVersionUID = 1L; 

      @Column(precision = 2) 
      private double paidAmount; 

      @Enumerated(EnumType.STRING) 
      private PaymentMethod paymentMethod; 

      private int checkNumber; 

      private String bank; 

      private String payer; 

      private String reciever; 

      private String notes; 

      @ManyToOne(cascade={CascadeType.PERSIST,CascadeType.MERGE}) 
      @JoinColumn(name = "fixRequestId") 
      private FixRequest fixRequest; 
     } 

     @Stateless 
     public class QueryFacade { 

      @PersistenceContext 
      EntityManager entityManager; 


     public List<UnpaidFixRequestObject> getUnpaidFixRequestObjects(long partyID){ 
      TypedQuery<UnpaidFixRequestObject> typedQuery = entityManager.createNamedQuery(FixRequest.getGetNotPaidRequestsByPartyQuery(),UnpaidFixRequestObject.class); 
      typedQuery.setParameter(FixRequest.getGetNotPaidRequestsByPartyQueryPartyParam(), partyID); 
      return typedQuery.getResultList(); 
     } 
     } 

     @ViewScoped 
     @ManagedBean 
     public class PaymentFace { 

      @EJB 
      private FootprintEntityFacade<Payment> paymentFacade; 

      @EJB 
      QueryFacade queryFacade; 

      private List<Party> parties; 
      private List<UnpaidFixRequestObject> unpaidFixRequests; 

      private String selectedParty = ""; 
      private String selectedFixRequest = ""; 
      private Payment payment; 

      private boolean showCreditPaymentControls = false; 

      @PostConstruct 
      public void init() { 
       parties = new ArrayList<Party>(); 
       unpaidFixRequests = new ArrayList<UnpaidFixRequestObject>(); 

       parties = queryFacade.getAllParties(); 
       for (Party p : parties) { 
        p.setName(p.getID() + " - " + p.getName() + " - " + p.getAddress()); 
       } 
      unpaidFixRequests = queryFacade.getUnpaidFixRequestObjects(parties.get(0).getID()); 
     } 
    } 

任何幫助,將不勝感激。 在此先感謝。

+0

將您的查詢,讓我們看看在那裏 –

+0

感謝什麼去您的回覆,下面是我最近我試圖查詢它返回null:選擇新org.master.jpa。 UnpaidFixRequestObject(v.attributes.type,v.attributes.model,fr.ID,fr.requestDate,SUM(f.cost),SUM(p.paidAmount))從Vehicle v,FixRequest fr,Payment p,Fix f,FixJoin fj WHERE v = fr.vehicle AND fr = p.fixRequest AND fr = fj.fixRequest AND fj.fix = f AND v.party.ID =:partyID GROUP BY v.attributes.type,v.attributes.model,fr。 ID,fr.requestDate –

+0

請在正確編輯的帖子中放置,也..因爲表很簡單,添加您的實體映射。讓我們看到整個圖片 –

回答

0

好的嘗試此查詢,我添加了一些明確連接:

"Select new org.master.jpa.UnpaidFixRequestObject(v.attributes.type,v.at‌​tributes.model,fr.ID‌​,fr.requestDate,SUM(‌​f.cost),SUM(p.paidAm‌​ount)) " + 
      " From FixRequest fr" + 
      " inner join fr.payments p" + 
      " inner join fr.vehicle v" + 
      " inner join v.party pt" + 
      " inner join fr.requset_Fixes rf" + 
      " inner join rf.fix f" 
      " WHERE pt.ID=:partyID " + 
      " GROUP BY v.attributes.type,v.attributes.model,fr.ID,fr.requestDate" 
+0

抱歉,但ID沒有工作因爲你似乎不能添加JOIN後表(實體)別名,請檢查圖像[鏈接](https://www.dropbox。 com/s/kp1tzqi6oyv44rl/Untitled1.png?dl = 0)@Maciej Kowalski –

+0

這個小錯字修正了一些錯誤,但仍有一些標有紅色的,幾乎完成請在這裏查看圖像[鏈接](https:/ /www.dropbox.com/s/wsodno8xb4yzdo2/Untitled2.png?dl=0)@Maciej Kowalski –

+0

運行查詢,讓我們看看在運行時會發生什麼。那些語法檢查工具有時是錯誤的。 –

0

寫的@Maciej查詢,並從SuperEntity的ID移動所有其他類更改模型。

實體層次結構的默認訪問類型由映射註釋放置在未明確指定訪問類型的實體類和映射超類的屬性上。

瞭解更多:Multiple @MappedSuperclass