Hibernate Criteria:嵌套异常是org.hibernate.QueryException:无法解析属性:

时间:2014-08-01 13:08:18

标签: java hibernate spring-mvc

我正在尝试从isssue表中获取与projectname和Issue Id匹配的所有数据。我收到了以下错误。请帮帮我。

表 的项目 专案编号 项目名

问题 issueID 识别码 sourceStatus 标题 严重 描述

Project.java

@Entity
    @Table(name="project")
    public class Project implements Serializable {

        private static final long serialVersionUID = 2457316470957669814L;
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name="ProjectId")
        private Integer projectId;

        @Column(name="projectname")
        private String projectName;

        @OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL,mappedBy = "project")
        private List<Issue> issue;

        //getters and setters
    }

Issue.java

@Entity
@Table(name="issue")
public class Issue implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="Id")
    private Integer Id;

    @Column(name="identifier")
    private String identifier;

    @Column(name="sourcestatus")
    private String sourceStatus;

    @Column(name="severity")
    private Integer severity;

    @Column(name="title")
    private String title;

    @Column(name="description")
    private String description;

    @ManyToOne
    @JoinColumn(name ="ProjectId")
    private Project project;

//getters and setters
  }

IssueDAOImpl.java

public List<Issue> getAllIssue(String identifier,String projectName) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Issue.class);
    criteria.createAlias("Project", "Prj");
    criteria.add(Restrictions.eq("identifier",identifier));
    criteria.add(Restrictions.eq("Prj.projectName",projectName));

    List<Issue> list=criteria.list();
    return list;
}

CONSOLE

SEVERE: Servlet.service() for servlet [dcm] in context with path [/SearchTool] threw exception [Request processing failed; nested exception is org.hibernate.QueryException: could not resolve property: Project of: com.dcm.search.bean.Issue] with root cause
org.hibernate.QueryException: could not resolve property: Project of: com.dcm.search.bean.Issue

2 个答案:

答案 0 :(得分:2)

criteria.createAlias("Project", "Prj")更改为criteria.createAlias("project", "Prj")。您尝试创建别名的属性是项目,因此应该作为createAlias的第一个参数。 项目是班级名称。

答案 1 :(得分:0)

你已经调用了Hibernate属性“ProjectId”,而不是“Project”。

criteria.createAlias("Project", "Prj");

这应该是:

criteria.createAlias("ProjectId", "Prj");