Hibernate namedquery错误参数id不作为[来自Employeenam e的e.name =:name]中的命名参数存在

时间:2013-02-12 05:49:56

标签: java hibernate named-query

我有两个类,一个POJO和一个主类,我在hibernate中使用带有注释的命名查询。

POJO类如下

@NamedQueries(
        {
            @NamedQuery(
                    name="findEmployeeName",
                        query="from Employeenam e where e.name=:name" 
                    )
        }
)
@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 setId() {
        return id;
    }

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

    public String setName() {
        return name;
    }

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

    public int getSalary() {
        return salary;
    }

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

    public String setJob() {
        return job;
    }

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

}

和主要类:

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");
        Employeenam e = new Employeenam();
        List<Employeenam> empList = query.list();
        session.close();
    }
}

我收到以下错误

Parameter id does not exist as a named parameter in [from Employeenam e where 
 e.name=:name]

有人可以帮我解决这个问题。我在哪里错了吗?

1 个答案:

答案 0 :(得分:3)

public int setId() 
{
    return id;
}

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

您错误地编写了ID的getter和setter方法。 getId应该返回int而不是setId。