0

我在hibernate中使用注释进行命名查询。我有两个类,一个POJO类和一个主类。POJO类如下

@NamedQuery(
    name="findEmployeeName",
    query="select * from employee " 
)


@Entity
@Table(name="employee")
public class Employeenam {
    public String tostring(){return id+" " +name+ " " +salary+ " " +job;}

    @Id
    @GeneratedValue
    @Column(name="id")
    int id;

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

    @Column(name="salary")
    int salary;

    @Column(name="job")
    String job;

    public int setempId(){
        return id;
    }

    public void getempId(int Id){
        this.id=Id;
    }

    public String setempName(){
        return name;
    }

    public void getempName(String Name){
        this.name=Name;
    }

    public int getempSalary(){
        return salary;
    }

    public void setempSalary(int Salary){
        this.salary=Salary;
    }

    public String setempJob(){
        return job;
    }

    public void getempJob(String Job){
        this.job=Job;
    }
}

and the main class as follows

    public class FetchData{
    public static void main(String[] args){

        Configuration configuration=new Configuration();
        configuration.configure("hibernate.cfg.xml");
        SessionFactory sfactory=configuration.buildSessionFactory();
        Session session=sfactory.openSession();
        Query query=session.getNamedQuery("findEmployeeName");
        query.setString("name", "dfdsf");
        query.setInteger("id", 34);
        query.setInteger("salary", 3543);
        query.setString("job", "dfgere");       
        session.close();
    }
    }

当我尝试运行此 iam 时出现以下错误

Exception in thread "main" org.hibernate.HibernateException: 
Errors in named queries: findEmployeeName

谁能告诉我哪里出错了??我对休眠有点陌生..所以请原谅我任何明显的错误....

4

2 回答 2

1

你在你的命名查询中错了试试下面

@NamedQueries({

    @NamedQuery(name="findEmployee",query="from Employeenam e" )
})

代替

@NamedQuery(name="findEmployeeName",query="select * from employee " )

在你的FetchData.java下面一个

    Configuration configuration=new Configuration();
    configuration.configure("hibernate.cfg.xml");
    SessionFactory sfactory=configuration.buildSessionFactory();
    Session session=sfactory.openSession();
    Query query=session.getNamedQuery("findEmployee");

   // here you will get the list of employees
    List<employee> empList = query.list();

    session.close();

更新

如果您需要参数化命名查询,那么试试这个

 @NamedQueries({

        @NamedQuery(name="findEmployeeByName",query="from Employeenam e where e.name =:name" )
    })

在 main 方法内部将查询行替换为这种方式

Query query=session.getNamedQuery("findEmployeeByName");

query.setString(¨name¨,¨subodh¨);    

       // here you will get the list of employees
        List<employee> empList = query.list();

        session.close();
于 2013-02-11T12:09:33.593 回答
1

With HQL, you should reference entities instead of tables. So, it should be Employeenam instead of employee. If you're setting parameters to the query, you should also be using them in the query, making reference to the property names, according to the getters/setters (for instance, empSalary).

Try to write your query like this:

from Employeenam 
where empname = :name 
    and empid = :id 
    and empsalary = :salary 
    and empjob = :empjob

Also, you might consider changing your property names to match the getters/setters. If the property is called id, the corresponding getter/setter should be getId()/setId(id).

于 2013-02-11T12:12:06.773 回答