I have created a MySQL Stored Procedure for a Student Class like this,I want to call this Stored Procedure in Hibernate
Stored Procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS `StudentProcedure`$$
CREATE PROCEDURE `StudentProcedure`(IN studentNo varchar(12) )
BEGIN
SELECT SNAME FROM Student WHERE SNO=studentNo;
END$$
DELIMITER ;
My Pojo class:
package edu.model;
public class Student {
private String studentNo;
private String studentName;
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
}
HBM File:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="edu.model.Student" table="Student">
<id name="studentNo" column="SNO">
<generator class="assigned"/>
</id>
<property name="studentName">
<column name="SNAME" />
</property>
</class>
<sql-query name="StudentProcedure" callable="true">
<return alias="student" class="edu.model.Student">
<return-property name="studentName" column="SNAME">
</return-property>
</return>
<![CDATA[CALL StudentProcedure(:studentNo)]]>
</sql-query>
</hibernate-mapping>
TestClass:
package edu.test;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import edu.model.Student;
import edu.util.SessionUtil;
public class ProcedureTest {
public static void main(String args[]) {
Session session = SessionUtil.getSession();
Transaction tx = session.beginTransaction();
try {
Query query = session.getNamedQuery("StudentProcedure");
System.out.println("Test");
query.setParameter("studentNo","123");
Student student = (Student)query.uniqueResult();
System.out.println("Student Name:" + student.getStudentName());
} catch (HibernateException e) {
System.err.println("Hibernate Exception>>>"+e.getClass().getName()+"..........+"+e.getMessage());
tx.rollback();
}
}
}
When I run My class I am facing the below Exception:
SEVERE: Column 'SNO0_0_' not found.
Hibernate Exception>>>org.hibernate.exception.SQLGrammarException..........+could not execute query
Could any one help me
thanks
http://blog.unidev.com/index.php/2009/03/03/calling-oracle-stored-procedure-from-hibernate/
You also need to define return property for the SNO