如何退回给定客户的所有保留订单

时间:2019-02-24 19:11:27

标签: java jpql

我有一个客户班

@Entity
@Table(name = "customers")
@NamedQueries({
@NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c")
, @NamedQuery(name = "Customer.findByCustomernumber", query = "SELECT c FROM 
Customer c WHERE c.customernumber = :customernumber")
, @NamedQuery(name = "Customer.findByCustomername", query = "SELECT c FROM 
Customer c WHERE c.customername = :customername")
, @NamedQuery(name = "Customer.findByContactlastname", query = "SELECT c 
FROM Customer c WHERE c.contactlastname = :contactlastname")
, @NamedQuery(name = "Customer.findByContactfirstname", query = "SELECT c 
FROM Customer c WHERE c.contactfirstname = :contactfirstname")
, @NamedQuery(name = "Customer.findByPhone", query = "SELECT c FROM Customer 
c WHERE c.phone = :phone")
, @NamedQuery(name = "Customer.findByAddressline1", query = "SELECT c FROM 
Customer c WHERE c.addressline1 = :addressline1")
, @NamedQuery(name = "Customer.findByAddressline2", query = "SELECT c FROM 
Customer c WHERE c.addressline2 = :addressline2")
, @NamedQuery(name = "Customer.findByCity", query = "SELECT c FROM Customer 
c WHERE c.city = :city")
, @NamedQuery(name = "Customer.findByState", query = "SELECT c FROM Customer 
c WHERE c.state = :state")
, @NamedQuery(name = "Customer.findByPostalcode", query = "SELECT c FROM 
Customer c WHERE c.postalcode = :postalcode")
, @NamedQuery(name = "Customer.findByCountry", query = "SELECT c FROM 
Customer c WHERE c.country = :country")
, @NamedQuery(name = "Customer.findByCreditlimit", query = "SELECT c FROM 
Customer c WHERE c.creditlimit = :creditlimit")})
public class Customer implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "customernumber", nullable = false)
private Integer customernumber;
@Column(name = "customername", length = 50)
private String customername;
@Column(name = "contactlastname", length = 50)
private String contactlastname;
@Column(name = "contactfirstname", length = 50)
private String contactfirstname;
@Column(name = "phone", length = 50)
private String phone;
@Column(name = "addressline1", length = 50)
private String addressline1;
@Column(name = "addressline2", length = 50)
private String addressline2;
@Column(name = "city", length = 50)
private String city;
@Column(name = "state", length = 50)
private String state;
@Column(name = "postalcode", length = 15)
private String postalcode;
@Column(name = "country", length = 50)
private String country;
// @Max(value=?)  @Min(value=?)//if you know range of your decimal fields 
consider using these annotations to enforce field validation
@Column(name = "creditlimit", precision = 22, scale = 0)
private Double creditlimit;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer")
private Collection<Payment> paymentCollection;
@OneToMany(mappedBy = "customer")
private Collection<OrderClassic> orderClassicCollection;
@JoinColumn(name = "salesrepemployeenumber", referencedColumnName = 
"employeenumber")
@ManyToOne

还有一个订单经典类

 @Entity
 @Table(name = "orders")
 @NamedQueries({
 @NamedQuery(name = "OrderClassic.findAll", query = "SELECT o FROM 
 OrderClassic o")
 , @NamedQuery(name = "OrderClassic.findByOrdernumber", query = "SELECT o 
 FROM OrderClassic o WHERE o.ordernumber = :ordernumber")
 , @NamedQuery(name = "OrderClassic.findByOrderdate", query = "SELECT o FROM 
OrderClassic o WHERE o.orderdate = :orderdate")
, @NamedQuery(name = "OrderClassic.findByRequireddate", query = "SELECT o 
FROM OrderClassic o WHERE o.requireddate = :requireddate")
, @NamedQuery(name = "OrderClassic.findByShippeddate", query = "SELECT o 
FROM OrderClassic o WHERE o.shippeddate = :shippeddate")
, @NamedQuery(name = "OrderClassic.findByStatus", query = "SELECT o FROM 
OrderClassic o WHERE o.status = :status")})
public class OrderClassic implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "ordernumber", nullable = false)
private Integer ordernumber;
@Column(name = "orderdate")
@Temporal(TemporalType.DATE)
private Date orderdate;
@Column(name = "requireddate")
@Temporal(TemporalType.DATE)
private Date requireddate;
@Column(name = "shippeddate")
@Temporal(TemporalType.DATE)
private Date shippeddate;
@Column(name = "status", length = 15)
private String status;
@Lob
@Column(name = "comments", length = 65535)
private String comments;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "orderClassic")
private Collection<Orderdetail> orderdetailCollection;
@JoinColumn(name = "customernumber", referencedColumnName = 
"customernumber")
@ManyToOne
private Customer customer;

我尝试编写此jpql查询以返回给定客户的所有保留订单。但这不起作用 我的jpql不正确。我还不清楚在JPQL中join是如何工作的。如果有人可以向我解释,那么现在有点混乱。我非常感激。也许除了sql以外,JPQL的一般区别还可以。

public List<Integer> getOrdersOnHold(int customerNumber){
         EntityManager em = emf.createEntityManager();
    try{
        em.getTransaction().begin(); 
         Query q = em.createQuery("select o.ordernumber From OrderClassic o 
  join c from Customer c on c.customernumber =:customernumber where o.status 
  ='On Hold'");
         q.setParameter("customernumber",customerNumber);
         List<Integer> numbers = q.getResultList();
        em.getTransaction().commit();
        return numbers;
    }finally{
                 em.close();
            }
    }

0 个答案:

没有答案