我正在開發一個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());
}
}
任何幫助,將不勝感激。 在此先感謝。
將您的查詢,讓我們看看在那裏 –
感謝什麼去您的回覆,下面是我最近我試圖查詢它返回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 –
請在正確編輯的帖子中放置,也..因爲表很簡單,添加您的實體映射。讓我們看到整個圖片 –