2013-05-29 54 views
1

我有疑問和問題如何在NamedQueries中使用正確的HQL,我閱讀了很多文章,並在項目中做了一些測試,但我遇到了一個問題,我不知道爲什麼我有這樣的問題,對我來說它的一切ok,看到堆棧跟蹤:如何在NamedQuery中使用正確的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); 
    } 
} 

是項目類相同的問題:

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

} 

謝謝!!

回答

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

好!我用':='犯了一個大錯誤。這是工作,謝謝! – Andriel

+0

@Andriel很高興我能幫到你。 –