gpt4 book ai didi

java - 如何在NamedQuery中使用正确的HQL

转载 作者:行者123 更新时间:2023-11-30 04:22:07 25 4
gpt4 key购买 nike

我对如何在 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

ProjectMediator 类:

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

}

谢谢!!

最佳答案

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")

关于java - 如何在NamedQuery中使用正确的HQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16809739/

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