JPA 查询语言标准生成器
Posted
技术标签:
【中文标题】JPA 查询语言标准生成器【英文标题】:JPA query language criteriaBuilder 【发布时间】:2011-06-12 00:25:01 【问题描述】:我在 EJB 容器中使用 JPA 构建了一个应用程序。 这是我的代码
@PersistenceContext(unitName = "damate-pu")
private EntityManager em;
@Override
public Workspace find(String username, String path)
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Workspace> criteriaQuery = criteriaBuilder.createQuery(Workspace.class);
Root<Workspace> from = criteriaQuery.from(Workspace.class);
Predicate condition = criteriaBuilder.equal(from.get("Username"), username);
Predicate condition2 = criteriaBuilder.equal(from.get("Path"), path);
Predicate condition3 = criteriaBuilder.and(condition, condition2);
criteriaQuery.where(condition3);
Query query = em.createQuery(criteriaQuery);
return (Workspace) query.getSingleResult();
当我尝试从 Web 服务运行此方法时,我收到以下错误:
java.lang.IllegalArgumentException: The attribute [Username] from the managed type....
可能是什么问题?我觉得from.get("Username")
有问题...
你怎么看?以及如何解决?
编辑:Workspace.java
package com.ubb.damate.model;
import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;
import java.util.Set;
/**
* The persistent class for the workspace database table.
*
*/
@Entity
@Table(name="workspace")
public class Workspace implements Serializable
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="WorkspaceId", unique=true, nullable=false)
private int workspaceId;
@Temporal( TemporalType.DATE)
@Column(name="CreationDate", nullable=false)
private Date creationDate;
@Lob()
@Column(name="Path", nullable=false)
private String path;
@Column(name="Username", nullable=false, length=20)
private String username;
//bi-directional many-to-one association to Project
@OneToMany(mappedBy="workspace")
private Set<Project> projects;
public Workspace()
public int getWorkspaceId()
return this.workspaceId;
public void setWorkspaceId(int workspaceId)
this.workspaceId = workspaceId;
public Date getCreationDate()
return this.creationDate;
public void setCreationDate(Date creationDate)
this.creationDate = creationDate;
public String getPath()
return this.path;
public void setPath(String path)
this.path = path;
public String getUsername()
return this.username;
public void setUsername(String username)
this.username = username;
public Set<Project> getProjects()
return this.projects;
public void setProjects(Set<Project> projects)
this.projects = projects;
【问题讨论】:
【参考方案1】:您是否尝试过使用元模型?
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
Metamodel m = em.getMetamodel();
EntityType<Workspace> WS = m.entity(Workspace.class);
CriteriaQuery<Workspace> criteriaQuery = criteriaBuilder.createQuery(Workspace.class);
Root<Workspace> from = criteriaQuery.from(Workspace.class);
Predicate condition = criteriaBuilder.equal(from.get(WS.username), username);
http://download.oracle.com/javaee/6/tutorial/doc/gjivm.html
【讨论】:
我无法从 WS 获取用户名。为什么会这样? 抱歉,错过了正确的 MetaModel 文档页面 download.oracle.com/javaee/6/tutorial/doc/gjiup.html【参考方案2】:在构建条件查询(或在字符串中构建 jpql)时,您希望使用实体属性名称,而不是列名称。您的数据库列名为“用户名”,但 Workspace 对象的属性是“用户名”,没有大写 U。
【讨论】:
【参考方案3】:CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<> criteriaQuery = criteriaBuilder
.createQuery(Date.class);
Root<test> root = criteriaQuery.from(test.class);
criteriaQuery.select(criteriaBuilder.greatest(root
.<Date> get("Starttime")));
criteriaQuery.where(
criteriaBuilder.equal(root.get("columnName 1"), filtervalue),
criteriaBuilder.equal(root.get("columnName 2"), Filtervalue));
TypedQuery<Date> query = entityManager.createQuery(criteriaQuery);
Date date = query.getSingleResult();
【讨论】:
以上是关于JPA 查询语言标准生成器的主要内容,如果未能解决你的问题,请参考以下文章