Showing posts with label composite key. Show all posts

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!