Spinning around for about half an hour, why query with JPA stumble EJB Exception merely. Whereas what I want to do is simple enough to take...

Spinning around for about half an hour, why query with JPA stumble EJB Exception merely. Whereas what I want to do is simple enough to take distinct date data. Apparently the hands and eyes do not go together: D

I have a table that consists of 2 primary keys and some additional columns, call it a content table with the following structure:


ColumnData TypePrimary
trsdatedateYes
custnumintYes
custnamevarcharNo
amountdoubleNo

As you can see, those trsdate and custnum column are primary key. Next, i create following entity classes based on those above table. 

 @Embeddable  
 public class ContentPK implements Serializable {  
   @Basic(optional = false)  
   @NotNull  
   @Column(name = "trsdate")  
   @Temporal(TemporalType.DATE)  
   private Date trsdate;  
   @Basic(optional = false)  
   @NotNull  
   @Column(name = "custnum")  
   private int custnum;  
   public ContentPK() {  
   }  
   public ContentPK(Date trsdate, int custnum) {  
     this.trsdate = trsdate;  
     this.custnum = custnum;  
   }  
   public Date getTrsdate() {  
     return trsdate;  
   }  
   public void setTrsdate(Date trsdate) {  
     this.trsdate = trsdate;  
   }  
   public int getCustnum() {  
     return custnum;  
   }  
   public void setCustnum(int custnum) {  
     this.custnum = custnum;  
   }  
   @Override  
   public int hashCode() {  
     int hash = 0;  
     hash += (trsdate != null ? trsdate.hashCode() : 0);  
     hash += (int) custnum;  
     return hash;  
   }  
   @Override  
   public boolean equals(Object object) {  
     // TODO: Warning - this method won't work in the case the id fields are not set  
     if (!(object instanceof ContentPK)) {  
       return false;  
     }  
     ContentPK other = (ContentPK) object;  
     if ((this.trsdate == null && other.trsdate != null) || (this.trsdate != null && !this.trsdate.equals(other.trsdate))) {  
       return false;  
     }  
     if (this.custnum != other.custnum) {  
       return false;  
     }  
     return true;  
     }  
     @Override  
     public String toString() {  
     return "ContentPK[ trsdate=" + trsdate + ", custnum=" + custnum + " ]";  
   }  
 }  

 @Entity  
 @Table(name = "content")  
 @XmlRootElement  
 public class Content implements Serializable {  
   private static final long serialVersionUID = 1L;  
   @EmbeddedId  
   protected ContentPK contentPK;  
   @Basic(optional = false)  
   @NotNull  
   @Size(min = 1, max = 100)  
   @Column(name = "custname")  
   private String custname;  
   @Basic(optional = false)  
   @NotNull  
   @Column(name = "amount")  
   private double amount;  
   public Content() {  
   }  
   public Content(ContentPK contentPK) {  
     this.contentPK = contentPK;  
   }  
   public Content(ContentPK contentPK, String custname, double amount) {  
     this.contentPK = contentPK;  
     this.custname = custname;  
     this.amount = amount;  
   }  
   public Content(Date trsdate, int custnum) {  
     this.contentPK = new ContentPK(trsdate, custnum);  
   }  
   public ContentPK getContentPK() {  
     return contentPK;  
   }  
   public void setContentPK(ContentPK contentPK) {  
     this.contentPK = contentPK;  
   }  
   public String getCustname() {  
     return custname;  
   }  
   public void setCustname(String custname) {  
     this.custname = custname;  
   }  
   public double getAmount() {  
     return amount;  
   }  
   public void setAmount(double amount) {  
     this.amount = amount;  
   }  
   @Override  
   public int hashCode() {  
     int hash = 0;  
     hash += (contentPK != null ? contentPK.hashCode() : 0);  
     return hash;  
   }  
   @Override  
   public boolean equals(Object object) {  
     // TODO: Warning - this method won't work in the case the id fields are not set  
     if (!(object instanceof Content)) {  
       return false;  
     }  
     Content other = (Content) object;  
     if ((this.contentPK == null && other.contentPK != null) || (this.contentPK != null && !this.contentPK.equals(other.contentPK))) {  
       return false;  
     }  
     return true;  
   }  
   @Override  
   public String toString() {  
     return "Content[ contentPK=" + contentPK + " ]";  
   }  
 }  

ContentPK class contains 2 of the primary key.

My purpose is to do below query

em.createQuery("SELECT DISTINCT c.contentPK.trsdate FROM Content c", Content.class).getResultList();

and then zapp, it works now!

Following are the steps you might try... download common-fileupload at http://commons.apache.org/fileupload. create web application proje...

Following are the steps you might try...
  1. download common-fileupload at http://commons.apache.org/fileupload.
  2. create web application project on netbeans
  3. add library common-fileupload and all dependencies (common-logging, common-beanutils)
  4. Create html form with file upload input in it, don't forget to set form enctype to multipart/form-data
  5. Create a servlet
  6. Create folder "destination" in the root web
  7. Create following script to handle upload file


1:    boolean isMultipart = ServletFileUpload.isMultipartContent(request);  
2:    if (isMultipart) {  
3:      try {  
4:        FileItemFactory factory = new DiskFileItemFactory();  
5:        ServletFileUpload upload = new ServletFileUpload(factory);  
6:        List items = upload.parseRequest(request);  
7:        Iterator iter = items.iterator();  
8:        String myfile = "";  
9:        while (iter.hasNext()) {  
10:          FileItem item = (FileItem) iter.next();  
11:          if (!item.isFormField()) {  
12:            myfile = item.getName();  
13:            File fullFile = new File(contract);  
14:            File savedFile = new File(getServletContext().getRealPath("/destination"),  
15:    fullFile.getName());  
16:            try {  
17:              item.write(savedFile);  
18:            } catch (Exception ex) {  
19:              Logger.getLogger(ControllerServlet.class.getName()).log(Level.SEVERE, null, ex);  
20:            }  
21:      }  
22:    }  

uploaded file should be available on "destination" folder :)