gpt4 book ai didi

java - 存储过程无法从 Hibernate 调用

转载 作者:行者123 更新时间:2023-11-29 06:15:25 26 4
gpt4 key购买 nike

我试图触发 MySQL 的存储过程,在 MySQL 中存储过程工作正常,但是当我尝试从 Hibernate 触发时,它给出了异常。

org.hibernate.MappingException: Named query not known: GetEmployeeDetails

这是我的代码。

//Stored procedure
Query q = session.getNamedQuery("GetEmployeeDetails").setParameter("empId", 10);
List<?> result = q.list();
for(int i = 0; i < result.size(); i++) {
Employee e = (Employee)result.get(i);
System.out.println("Employee Name :"+e.getName());
}

我的 Employee 类是。

import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@Entity
public class Employee {

@Id
private int id;
private String name;


public Employee() {
}

public Employee(int id, String name) {
super();
this.id = id;
this.name = name;
}

public int getId() {
return id;
}

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

public String getName() {
return name;
}

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


}

我的存储过程

CREATE DEFINER=`root`@`localhost` PROCEDURE `GetEmployeeDetails`(empid  varchar(20))
BEGIN
select * from employee where id=empid;
END

最佳答案

您的代码似乎没问题。可能是 mysql 端的一些其他错误。否则使用其他替代方法来调用存储过程,如下所示。

Query query = session.createSQLQuery("CALL GetEmployeeDetails(:empId)").addEntity(Employee.class).setParameter("empId", 10); 

查看更多信息:http://www.mkyong.com/hibernate/how-to-call-store-procedure-in-hibernate/

关于java - 存储过程无法从 Hibernate 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36145807/

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