Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to insert Data using SP from hibernate

 
Samrat Gadamsetty
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Please help me in calling stored procedures from hibernate which will take 4 parameters.

My mapping.hbm.xml file :

<class name="com.invoice.connection.Invoice2" table="SamratTest">
<id column="empid" name="empid"/>
<property name="empName" >
<column name="EmpName"/>
</property>

<property name="ecreatedby">
<column name="CreatedBy"/>
</property>
<property name="empsal" >
<column name="Salary"/>
</property>
<property name="empcate">
<column name="Category"/>
</property>
<sql-insert callable="true" check="none">{ call insert_SamratTest_1(?,?,?,?) }</sql-insert>
</class>

** only EmpName,CreatedBy,Salary and Category should be the parameters for the SP and as id tag is mandatory i have mapped that to PK

My java code to call SP:

Session session=conn.takeConnect();
try
{
Transaction tx=session.beginTransaction();
Invoice2 inv2=new Invoice2();
inv2.setEmpName("Samrat");
inv2.setEmpid(1);
inv2.setEmpdesg("software");
inv2.setEmpsal(1);
inv2.setEcreatedby(1);
session.save(inv2);
tx.commit();
}catch(Exception e)
{
e.printStackTrace();
}

Please help me to get out of the following issue ......


org.hibernate.exception.GenericJDBCException: could not insert: [com.invoice.connection.Invoice2]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
org.hibernate.exception.GenericJDBCException: could not insert: [com.invoice.connection.Invoice2]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
....

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The index 5 is out of range.
at java.lang.Thread.run(Thread.java:619)
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)


Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The index 5 is out of range.
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setterGetParam(SQLServerPreparedStatement.java:698)
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)


Thanks in advance
 
Samrat Gadamsetty
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got stuck here... Please someone help me..
 
Justin Chi
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are 5 attributes: empid,empName,ecreatedby,empsal and empcate, but you only have 4 "?" as input parameters.
 
Samrat Gadamsetty
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Chi wrote:There are 5 attributes: empid,empName,ecreatedby,empsal and empcate, but you only have 4 "?" as input parameters.


Thanks for Reply. The empid is autogenerated in the database...

MY SP is
ALTER PROCEDURE [insert_SamratTest_1]
(@EmpName_1 [varchar](100),

@Category_2 [varchar](100),

@Salary_3 [smallint],

@CreatedBy_4 [smallint])


AS INSERT INTO [dbo].[SamratTest]

( [EmpName],

[Category],

[Salary],

[CreatedBy],Createdate)


VALUES

( @EmpName_1,

@Category_2,

@Salary_3,

@CreatedBy_4, Getdate())



 
Samrat Gadamsetty
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pretty Intresting thing went...

I updated my SP to return a value if insert operation goes success..

Accordingly I changed my mapping.cfg.xml file to
<class name="com.invoice.connection.Invoice2" table="SamratTest">
<id column="empid" name="empid" >
<generator class="increment"></generator>
</id>
<property name="empName" insert="true" >
<column name="EmpName"/>
</property>
<property name="empdesg" insert="true">
<column name="Category"/>
</property>
<property name="ecreatedby" insert="true">
<column name="CreatedBy"/>
</property>
<property name="empsal" insert="true" >
<column name="Salary"/>
</property>

<sql-insert callable="true" check="none">{?=call insert_SamratTest_1(?,?,?,?) }</sql-insert>
</class>


And My java class to


Session session=conn.takeConnect();
try
{
Transaction tx=session.beginTransaction();
Invoice2 inv2=new Invoice2();
inv2.setEmpName("Samrat");

inv2.setEmpdesg("software");
inv2.setEmpid(84);
inv2.setEmpsal(1);
inv2.setEcreatedby(1);
System.out.println("******************BEFore saving");
session.save(inv2);
tx.commit();
System.out.println("******************After saving");

}

Here I am able to run my SP successfully from hibernate but the record inserted in database is intrestingly changed...

My Database Result
EMPID EMPNAME Category Salary Created By Date
1 samrat software 1 1 2011-02-12 14:28:00
4 dff software 1 1 2011-02-12 17:16:00
7 samrat software 1 1 2011-02-14 11:57:00
8 samrat software 1 1 2011-02-14 11:57:00
9 software 1 1 9 2011-02-14 14:07:00
10 software 1 1 10 2011-02-14 14:11:00
11 software 1 1 11 2011-02-14 14:13:00

What happened to my Name?
 
Samrat Gadamsetty
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My luck is here...



<sql-insert callable="true" check="none">{call insert_SamratTest_1(?,?,?,?,?) }</sql-insert>

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic