0

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

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]

有人可以帮我解决这个问题..我又哪里出错了?

4

1 回答 1

3
public int setId() 
{
    return id;
}

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

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

于 2013-02-12T08:32:32.037 回答