-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Problem in calling Parameterised Stroed Procedure
PostPosted: Tue Apr 25, 2006 6:19 am 
Newbie

Joined: Mon Apr 17, 2006 5:01 am
Posts: 4
I executed stored procedure which doesn't have any parameters through Hibernate successfully, but I have problem while sending the parameters to stored procedure..

I would like to share one more exception, which I am facing while calling the stored procedure with Parameters (from Oracle).

Here I am listing the stored procedure what I have created for testing purpose in Oracle database. I am sending Manager, value Object, .hbm file what I created as an attachment to this mail. Please let me know where i am doing the mistake.

Here i am facing the problem 1) In sending parameters ? 2) writing value object, may be i need to implement any other interface? 3) .hbm file which i attached



CREATE OR REPLACE FUNCTION Selectallemploymentsparam(empno1 IN number)
RETURN SYS_REFCURSOR
AS
st_cursor SYS_REFCURSOR;
BEGIN
OPEN st_cursor FOR
SELECT empno,empname
FROM EMP where empno=empno1;
RETURN st_cursor;
END;


Exception is Like this..

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
after sessionFactory
after opening session.... before calling query
org.hibernate.MappingException: Unknown entity: Employee
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:569)
at org.hibernate.loader.custom.SQLQueryReturnProcessor.getSQLLoadable(SQLQueryReturnProcessor.java:53)
at org.hibernate.loader.custom.SQLQueryReturnProcessor.processRootReturn(SQLQueryReturnProcessor.java:119)
at org.hibernate.loader.custom.SQLQueryReturnProcessor.processReturn(SQLQueryReturnProcessor.java:98)
at org.hibernate.loader.custom.SQLQueryReturnProcessor.process(SQLQueryReturnProcessor.java:87)
at org.hibernate.loader.custom.SQLCustomQuery.<init>(SQLCustomQuery.java:105)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:144)
at ParamManager.main(ParamManager.java:39)


Value Object ::

import java.sql.SQLData;
import java.sql.SQLException;
import java.sql.SQLInput;
import java.sql.SQLOutput;


public class Employee implements SQLData {
/**
*
* This attribute holds venderName
*/
private int empno ;
private String empname = null;



/**
* @return Returns the empname.
*/
public String getEmpname() {
return empname;
}
/**
* @param empname The empname to set.
*/
public void setEmpname(String empname) {
this.empname = empname;
}
/**
* @return Returns the empno.
*/
public int getEmpno() {
return empno;
}
/**
* @param empno The empno to set.
*/
public void setEmpno(int empno) {
this.empno = empno;
}
/* (non-Javadoc)
* @see java.sql.SQLData#getSQLTypeName()
*/
public String getSQLTypeName() throws SQLException {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see java.sql.SQLData#readSQL(java.sql.SQLInput, java.lang.String)
*/
public void readSQL(SQLInput stream, String typeName) throws SQLException {
empname = stream.readString();
empno = stream.readInt();

}
/* (non-Javadoc)
* @see java.sql.SQLData#writeSQL(java.sql.SQLOutput)
*/
public void writeSQL(SQLOutput stream) throws SQLException {
// TODO Auto-generated method stub
stream.writeString(empname);
stream.writeInt(empno);

}

}


Persistence manager::

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/*
* Created on Apr 20, 2006
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author jbhogi
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class ParamManager {

public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
System.out.println("after sessionFactory");
Session session = sessionFactory.openSession();
System.out.println("after opening session.... before calling query");
session.beginTransaction();
Employee e = new Employee();
e.setEmpno(98);
try {

Query q = session.getNamedQuery("Selectallemploymentsparam_SP");
q.setInteger(1, e.getEmpno());
List l = q.list();
System.out.println("size of the List ::::" + l.size());
Iterator itr = l.iterator();

while (itr.hasNext()) {
Employee emp = (Employee) itr.next();
if (emp != null) {
System.out.println("Employee Number ::: " + emp.getEmpno());
System.out.println("Employee Name :::" + emp.getEmpname());
System.out.println("-------------------------------------");
}
}
} catch (HibernateException ex) {
ex.printStackTrace();
} finally {
session.close();
}
}
}



.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>
<sql-query name="Selectallemploymentsparam_SP" callable="true">
<return alias="emp" class="Employee">
<return-property name="empno" column="empno"/>
<return-property name="empname" column="empname" />
</return>
{? = call Selectallemploymentsparam(?)}
</sql-query>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 7:18 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
I think you need a mapping file like this:

Code:
<?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="Employee">
      <id name="empno" column="empno" type="integer">
         <generator class="native"/>
      </id>
      <property name="empname" column="empname" type="string"/>
   </class>
   
   <sql-query name="Selectallemploymentsparam_SP" callable="true">
      <return alias="emp" class="Employee">
         <return-property name="empno" column="empno"/>
         <return-property name="empname" column="empname" />
      </return>
      {? = call Selectallemploymentsparam(?)}
   </sql-query>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Problem Solved !!! If we do Like this with stored procedure
PostPosted: Fri Apr 28, 2006 1:16 am 
Newbie

Joined: Mon Apr 17, 2006 5:01 am
Posts: 4
Thanx for the reply... Still we had a problem even i changed the hbm file, we tried out like this, Now i am able to get the result..


/*
* Created on Apr 26, 2006
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package hibernateSample1;

import java.sql.Connection;

import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
* @author vreddy
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/

public class StoredProc {
public static void main(String[] args) {

SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();

System.out.println("after sessionFactory");

Session session = sessionFactory.openSession();

System.out.println("after opening session.... before calling query");

Connection connection = session.connection();

try {
//Map map = connection.getTypeMap();


Class c = Class.forName("hibernateSample1.NewUser");

System.out.println("class created ::::" + c.getName());

String queryName = "{? = call testfucntion(?)}";

NewUser user = new NewUser();

user.setEmpno(111);

OracleCallableStatement call = (OracleCallableStatement) connection.prepareCall(queryName);

System.out.println("call is like this::::::" + call);

call.registerOutParameter(1, OracleTypes.VARCHAR);

call.setInt(2, user.getEmpno());

boolean t = call.execute();


String empname = call.getString(1);

System.out.println("employee name:::" + empname);

call.close();

} catch (Exception e) {

e.printStackTrace();
}

}

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.