1

我对如何在 NamedQueries 中使用正确的 HQL 有疑问和问题,我阅读了很多文章并在我的项目中进行了一些测试,但是我有一个问题,我不知道为什么我会遇到这个问题,对我来说一切都好,请参阅堆栈跟踪:

SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/EMS2App] threw exception [java.lang.ExceptionInInitializerError] with root cause
org.hibernate.HibernateException: Errors in named queries: ProjectMediator.findProjectByIdProjectMediator, Project.findProjectWithStatus

项目中介类:

@Entity
@NamedQueries({
    @NamedQuery(name = "ProjectMediator.findProjectByIdProjectMediator",
                query = "SELECT pm FROM ProjectMediator pm WHERE pm.project.id := idProject") 
                        })

public class ProjectMediator implements Serializable {
/**
 *
 */
private static final long serialVersionUID = 1L;

public static final String FIND_PROJECT_BY_ID_PROJECTMEDIATOR = "ProjectMediator.findProjectByIdProjectMediator";

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@OneToOne
@JoinColumn(name="id_project")
private Project project;

ProjectMediatorDAO 类:

public class ProjectMediatorDAO extends GenericDAO<ProjectMediator>{

    private static final long serialVersionUID = 1L;

    public ProjectMediatorDAO () {
        super(ProjectMediator.class);
    }

    public ProjectMediator findProjectByIdProjectMediator (int idProject) {
        Map<String,Object> parameters = new HashMap<String,Object>();
        parameters.put("idProject",idProject);
        return super.findOneResult(ProjectMediator.FIND_PROJECT_BY_ID_PROJECTMEDIATOR, parameters);
    }
}

Project 类是否存在同样的问题:

@Entity
@NamedQueries ({
    @NamedQuery (name="Project.findProjectWithStatus",query="SELECT p FROM Project p WHERE p.statusProject := statusProject"),
})
public class Project implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final String FIND_PROJECT_WITH_STATUS = "Project.findProjectWithStatus";

    //Attributes
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Column(name="STATUS_PROJECT",columnDefinition="BIT", length=1)
    private Boolean statusProject;
  //gets and sets
}

ProjectDAO 类:

public class ProjectDAO extends GenericDAO<Project> {

    private static final long serialVersionUID = 1L;

    public ProjectDAO() {
        super(Project.class);
    }

    public List<Project> findProjectWithStatus(Boolean statusProject) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("statusProject", statusProject);
        return super.findManyResult(Project.FIND_PROJECT_WITH_STATUS,parameters);
    }

}

谢谢!!

4

1 回答 1

2

HQL 的语法已关闭。出现:在参数之前,=是静态查询的一部分。

改变:

SELECT pm FROM ProjectMediator pm WHERE pm.project.id := idProject

SELECT pm FROM ProjectMediator pm WHERE pm.project.id = :idProject

此问题也存在于您的第二个 HQL 查询中。

 @NamedQuery (name="Project.findProjectWithStatus",
     query="SELECT p FROM Project p WHERE p.statusProject := statusProject")

更正表格

 @NamedQuery (name="Project.findProjectWithStatus",
    query="SELECT p FROM Project p WHERE p.statusProject = :statusProject")
于 2013-05-29T09:07:40.863 回答