質問

私はJPA 2.0からクライテリアAPIでクエリを作成しようとしているが、私はそれを動作させることはできません。

問題は、条件付きメソッド「間」です。私は、私はそれをしなければならない方法を知っているいくつかのドキュメントを読みます私はJPAを発見していますので、それが動作しない理由はなく、私は理解していない。

まず、私は私が書くときに表示されます「のCreationDate」を参照してくださいすることはできません「トランザクションを_を。」

私は、メタモデルは、実行時に生成された読み以来、私は「Foo_.getDeclaredSingularAttribute( 『値』)」の代わりの「Foo_.value」を使用しようとしたので、それは、おそらく通常のだと思ったが、それはまだ仕事をしませんすべてのます。

ここに私のコードは次のとおりです。

public List<Transaction> getTransactions(Date startDate, Date endDate) {
    EntityManager em = getEntityManager();
    try {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Transaction> cq = cb.createQuery(Transaction.class);
        Metamodel m = em.getMetamodel();
        EntityType<Transaction> Transaction_ = m.entity(Transaction.class);
        Root<Transaction> 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<Transaction> result = em.createQuery(cq).getResultList();
        return result;
    } finally {
        em.close();
    }
}

缶誰かの助けが私はこれを理解するには?おかげます。

編集:ここに(それは自動的に私のデータベースから、Netbeansのによって生成されたので、それにはほとんど何も、)トランザクションのソースである

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 && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "projetjava.db.Transaction[id=" + id + "]";
    }

}
役に立ちましたか?

解決

  私は、メタモデルは、実行時に生成された読み以来、それは、おそらく通常のだと思った(...)

メタモデルクラスを使用してコンパイル時に生成されます。の注釈処理の。言い換えれば、あなたは、コンパイラレベルでアクティブ注釈処理する必要があります。 にHibernate JPA 2メタモデルジェネレータのドキュメントでは、方法を説明しますアリ、MavenとEclipseやアイデアのようなIDEでそれを行うには(アプローチは、他のプロバイダに置き換えることができます)。悲しいことに、この機能は現在のNetBeansではサポートされていません。

だから、どちらかの他のIDEに上記のビルドツールやスイッチを使用して構成する1。例えば、Eclipseので、の を右クリックし、[プロジェクトのとの Javaコンパイラ>注釈処理と、それを有効にします:

altテキスト

そして、を工場出荷時のパスのへの(このステップのためのあなたのJPAプロバイダのマニュアルを参照してください)プロバイダの必要なJAR(複数可)を追加します。

他のヒント

私はここで混乱部分があると思います q.where(cb.between(transaction.get(Transaction_.creationDate), startDate, endDate));

あなたは、このケースでTransaction_は、元のトランザクションのエンティティクラスに対応する静的インスタンス化、正規のメタモデルクラスがあることに注意しなければなりません。あなたはJPAライブラリを使用してTransaction_クラスをコンパイルしてTransactionクラスを生成する必要があります。 一つの有用なリンクは日食のためにここにあります: http://wiki.eclipse.org/UserGuide/JPA/Using_the_Canonical_Model_Generator_%28ELUG% 29

IntelliJ IDEAのために

ます。http:// blogs.jetbrains.com/idea/2009/11/userfriendly-annotation-processing-support-jpa-20-metamodel/する

JPAで、クエリの開始日と終了日

public List<Student> findStudentByReports(String className, Date startDate, Date endDate) {
    System.out
    .println("call findStudentMethd******************with this pattern"
            + className
            + startDate
            + endDate
            + "*********************************************");

    return em
    .createQuery(
            "select attendence from Attendence attendence where lower(attendence.className) like '"
            + className + "' or attendence.admissionDate BETWEEN : startdate AND endDate " + "'")
            .setParameter("startDate", startDate, TemporalType.DATE)
            .setParameter("endDate", endDate, TemporalType.DATE)
            .getResultList();
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top