【问题标题】:Stored procedure not able to call from Hibernate存储过程无法从 Hibernate 调用
【发布时间】:2016-03-22 03:52:57
【问题描述】:

我正在尝试触发 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

【问题讨论】:

    标签: java mysql hibernate stored-procedures


    【解决方案1】:

    您的代码似乎没问题。可能是 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/

    【讨论】:

    • @Syed 要么你撒谎,要么你把事情搞砸了。调用createSQLQuery 不会以org.hibernate.MappingException: Named query not known: GetEmployeeDetails 异常结束
    • 你如何将你的mysql表映射到hibernate模型类。你需要将你的模型类及其字段映射到相应的mysql表及其字段。你可以使用xml也可以使用@Table( name = "employee") 注释将表与表中的每个字段的类和 @Column(name=" ") 映射。
    • 您可以使用我在帖子中提供的链接中指定的 Employee.hbm.xml 映射您的存储过程。
    • ibernate: CALL GetEmployeeDetails(?) 2016-03-22 10:11:37 ERROR EmployeeImpl:203 - org.hibernate.exception.GenericJDBCException: could not extract ResultSet
    【解决方案2】:

    您需要为您的 Employee 类定义命名查询。

    import javax.persistence.Entity;
    import javax.persistence.Id;
    import org.hibernate.annotations.Cache;
    import org.hibernate.annotations.CacheConcurrencyStrategy;
    
    @NamedNativeQueries({
        @NamedNativeQuery(
            name = "GetEmployeeDetails",
            query = "CALL GetEmployeeDetails(:empId)",
            resultClass = Employee.class
        )
    })
    @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;
        }
    }
    

    【讨论】:

    • 你能告诉我我需要从课堂上删除代码吗?
    • 不,您只需要添加@NamedNativeQueries 部分。我更新了代码看一看。
    • 我需要使用id还是empid?
    • empId,因为您将参数设置为empId - setParameter("empId", 10);
    • 这仍然给我同样的错误:( Hibernate: CALL GetEmployeeDetails(?) 2016-03-22 10:11:37 ERROR EmployeeImpl:203 - org.hibernate.exception.GenericJDBCException: could not提取结果集
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    相关资源
    最近更新 更多