gpt4 book ai didi

java - Hibernate 命名查询错误参数 id 不作为命名参数存在于 [from Employeenam e where e.name= :name]

转载 作者:行者123 更新时间:2023-12-02 00:01:11 24 4
gpt4 key购买 nike

我有两个类,一个 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]

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

最佳答案

public int setId() 
{
return id;
}

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

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

关于java - Hibernate 命名查询错误参数 id 不作为命名参数存在于 [from Employeenam e where e.name= :name],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14826262/

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