gpt4 book ai didi

java - 使用 Criteria API (JPA 2.0) 创建查询

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:21:06 24 4
gpt4 key购买 nike

我正在尝试使用 JPA 2.0 中的 Criteria API 创建查询,但我无法让它工作。

问题出在“between”条件方法上。我读了some documentation知道我必须怎么做,但由于我发现了 JPA,所以我不明白为什么它不起作用。

首先,我看不到在我写“Transaction_”时应该出现的“creationDate”。

我认为这可能是正常的,因为我读到元模型是在运行时生成的,所以我尝试使用 'Foo_.getDeclaredSingularAttribute("value")' 而不是 '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 + "]";
}

}

最佳答案

I thought it was maybe normal, since I read the metamodel was generated at runtime (...)

元模型类是在编译时使用注解处理生成的。换句话说,您需要在编译器级别激活注释处理。 Hibernate JPA 2 Metamodel Generator文档描述了如何使用 Ant、Maven 和像 Eclipse 或 Idea 这样的 IDE 来做到这一点(该方法可以转移到其他供应商)。遗憾的是,NetBeans 目前不支持此功能。

因此,要么使用和配置上述构建工具之一,要么切换到另一个 IDE。例如,使用 Eclipse,右键单击项目并转到Java Compiler > Annotation Processing并启用它:

alt text

然后将您的提供程序所需的 JAR(请参阅此步骤的 JPA 提供程序的文档)添加到工厂路径

关于java - 使用 Criteria API (JPA 2.0) 创建查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2602757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com