JPA查询语言criteriaBuilder [英] JPA query language criteriaBuilder

查看:125
本文介绍了JPA查询语言criteriaBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在EJB容器中使用JPA构建了一个应用程序。
这里是我的代码

$ $ $ $
$ b $ private私人EntityManager em;

@Override
public Workspace find(String username,String path){
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<工作空间> criteriaQuery = criteriaBuilder.createQuery(Workspace.class);
Root<工作空间> from = criteriaQuery.from(Workspace.class);
谓词条件= criteriaBuilder.equal(from.get(用户名),用户名);
谓词condition2 = criteriaBuilder.equal(from.get(Path),path);
谓词条件3 = criteriaBuilder.and(condition,condition2);
criteriaQuery.where(condition3);
Query query = em.createQuery(criteriaQuery);

return(工作空间)query.getSingleResult();
}

当我尝试从web服务运行此方法时,出现以下错误:
java.lang.IllegalArgumentException:来自托管类型的属性[用户名] ....



可能是什么问题?我想我的 from.get(用户名) ...

有什么问题?和如何解决它?






编辑:Workspace.java

  package com.ubb.damate.model; 

import java.io.Serializable;
import javax.persistence。*;
import java.util.Date;
import java.util.Set;


/ **
*工作区数据库表的持久类。
*
* /
@Entity
@Table(name =workspace)
public class Workspace实现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创建日期;

@Lob()
@Column(name =Path,nullable = false)
private String path;

@Column(name =Username,nullable = false,length = 20)
private String username;

//双向多对一关联到Project
@OneToMany(mappedBy =workspace)
private设置< Project>项目; (){
}

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(){
返回this.projects;
}

public void setProjects(Set< Project> projects){
this.projects = projects;
}
}


解决方案

构建标准查询(或在字符串中构建jpql),您想要使用实体属性名称,而不是列名称。您的数据库列名为用户名,但Workspace对象的属性为用户名,不带大写字母U。


I have built an application using JPA in an EJB container. Here's my code

@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();
}

When I try to run this method from a webservice I get the following error: java.lang.IllegalArgumentException: The attribute [Username] from the managed type....

What can be the problem? I think I have a problem with from.get("Username")...
What do you think? And how to fix it?


Edit: 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;
    }
}

解决方案

When building your criteria query (or building jpql in a string), you want to use the entity property names, not the column names. Your database column is named "Username", but the property of the Workspace object is "username" without the capital U.

这篇关于JPA查询语言criteriaBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆