Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating queries using Criteria API (JPA 2.0)
    primarykey
    data
    text
    <p>I'm trying to create a query with the Criteria API from JPA 2.0, but I can't make it work.</p> <p>The problem is with the "between" conditional method. I read <a href="http://docs.sun.com/app/docs/doc/820-7627/gjitv" rel="noreferrer">some documentation</a> to know how I have to do it, but since I'm discovering JPA, I don't understand why it does not work.</p> <p>First, I can't see "creationDate" which should appear when I write "Transaction_."</p> <p>I thought it was maybe normal, since I read the metamodel was generated at runtime, so I tried to use 'Foo_.getDeclaredSingularAttribute("value")' instead of 'Foo_.value', but it still doesn't work at all.</p> <p>Here is my code :</p> <pre><code>public List&lt;Transaction&gt; getTransactions(Date startDate, Date endDate) { EntityManager em = getEntityManager(); try { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery&lt;Transaction&gt; cq = cb.createQuery(Transaction.class); Metamodel m = em.getMetamodel(); EntityType&lt;Transaction&gt; Transaction_ = m.entity(Transaction.class); Root&lt;Transaction&gt; transaction = cq.from(Transaction.class); // Error here. cannot find symbol. symbol: variable creationDate cq.where(cb.between(transaction.get(Transaction_.creationDate), startDate, endDate)); // I also tried this: // cq.where(cb.between(Transaction_.getDeclaredSingularAttribute("creationDate"), startDate, endDate)); List&lt;Transaction&gt; result = em.createQuery(cq).getResultList(); return result; } finally { em.close(); } } </code></pre> <p>Can someone help me to figure this out? Thanks.</p> <p>EDIT : here is the Transaction source (almost nothing in it, since it was automatically generated by Netbeans, from my database)</p> <pre><code>package projetjava.db; import java.io.Serializable; import java.util.Date; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity @Table(name = "transaction") @NamedQueries({ @NamedQuery(name = "Transaction.findAll", query = "SELECT t FROM Transaction t"), @NamedQuery(name = "Transaction.findById", query = "SELECT t FROM Transaction t WHERE t.id = :id"), @NamedQuery(name = "Transaction.findByIDAccount", query = "SELECT t FROM Transaction t WHERE t.iDAccount = :iDAccount"), @NamedQuery(name = "Transaction.findByDescription", query = "SELECT t FROM Transaction t WHERE t.description = :description"), @NamedQuery(name = "Transaction.findByCreationDate", query = "SELECT t FROM Transaction t WHERE t.creationDate = :creationDate"), @NamedQuery(name = "Transaction.findByAmount", query = "SELECT t FROM Transaction t WHERE t.amount = :amount")}) public class Transaction implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "ID") private Integer id; @Basic(optional = false) @Column(name = "IDAccount") private int iDAccount; @Basic(optional = false) @Column(name = "Description") private String description; @Basic(optional = false) @Column(name = "CreationDate") @Temporal(TemporalType.DATE) private Date creationDate; @Basic(optional = false) @Column(name = "Amount") private double amount; public Transaction() { } public Transaction(Integer id) { this.id = id; } public Transaction(Integer id, int iDAccount, String description, Date creationDate, double amount) { this.id = id; this.iDAccount = iDAccount; this.description = description; this.creationDate = creationDate; this.amount = amount; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public int getIDAccount() { return iDAccount; } public void setIDAccount(int iDAccount) { this.iDAccount = iDAccount; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Date getCreationDate() { return creationDate; } public void setCreationDate(Date creationDate) { this.creationDate = creationDate; } public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } @Override public int hashCode() { int hash = 0; hash += (id != null ? id.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 Transaction)) { return false; } Transaction other = (Transaction) object; if ((this.id == null &amp;&amp; other.id != null) || (this.id != null &amp;&amp; !this.id.equals(other.id))) { return false; } return true; } @Override public String toString() { return "projetjava.db.Transaction[id=" + id + "]"; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload