• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hibernate-sqlserver-select query

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi techies,
i am new to hibernate, my query is i am trying to select a record from sql-server database.
but getting following errors:

org.hibernate.exception.GenericJDBCException: could not extract ResultSet
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:75)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:53)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2036)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1836)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1815)
at org.hibernate.loader.Loader.doQuery(Loader.java:899)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
at org.hibernate.loader.Loader.doList(Loader.java:2522)
at org.hibernate.loader.Loader.doList(Loader.java:2508)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2338)
at org.hibernate.loader.Loader.list(Loader.java:2333)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:338)
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1827)
at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:231)
at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:157)
at com.infosys.dynacred.Client.main(Client.java:26)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The value is not set for the parameter number 1.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.buildParamTypeDefinitions(SQLServerPreparedStatement.java:262)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.buildPreparedStrings(SQLServerPreparedStatement.java:221)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doPrepExec(SQLServerPreparedStatement.java:598)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:386)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:340)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.execute(SQLServerPreparedStatement.java:322)
at org.hibernate.dialect.AbstractTransactSQLDialect.getResultSet(AbstractTransactSQLDialect.java:206)
at org.hibernate.dialect.SQLServerDialect.getResultSet(SQLServerDialect.java:41)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:69)
... 15 more

and below is my code base:

***client code***


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

import org.hibernate.*;
import org.hibernate.cfg.*;





public class Client {
public static void main(String args[]){
Transaction tx = null;
try{
SessionFactory sf = Hibernateutil.getSessionFactory();
Session session = sf.openSession();
tx = session.beginTransaction();
List result;

Query query = session.getNamedQuery("PROCEDURENAME");
result = query.list();
User user = (User)result.get(0);
System.out.println(user.getUserID());
tx.commit();
session.close();
sf.close();
}catch(Exception e){
e.printStackTrace();
if(tx != null)
tx.rollback();
}
}

}

***user.hbm.xml***

<?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 package = "com.#####.*****.beans">
<class name = "User" table = "Users" >
<id name = "userID" column = "USER_ID">
</id>
<property name="userName" column="USER_NAME" />
<property name="empNo" column="EMPLOYEE_NO" />
</class>

<sql-query name="PROCEDURENAME" callable="true">
<return class ="User">
<return-property name="userID" column="USER_ID" />
<return-property name="userName" column="USER_NAME" />
<return-property name="empNo" column="EMPLOYEE_NO" />
</return>
{ ? = call sp_sample_add_user() }
</sql-query>
</hibernate-mapping>

***user.java***



import java.util.ArrayList;

public class User {

private String userID;
private String userName;
private int empNo;
public User(){

}
public User(String userID,String userName, int empNo){
this.userID = userID;
this.userName = userName;
this.empNo = empNo;
}


/**
* @return the userID
*/
public String getUserID() {
return userID;
}
/**
* @param userID the userID to set
*/
public void setUserID(String userID) {
this.userID = userID;
}
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the empNo
*/
public int getEmpNo() {
return empNo;
}
public void setEmpNo(int empNo) {
this.empNo = empNo;
}


/*public String toString(){
String userStr = "{"+userID+" "+userName+"}";
return null;
}*/
}

***stored procedure***

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- PROCEDURE FOR ADD USER --
ALTER PROC [dbo].[sp_sample_add_user]
AS
BEGIN
SELECT * FROM Users
RETURN
END



please help me to proceed further.
thanks in advance.
 
money grubbing section goes here:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic