-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to Pass primitive type in Query.setParameter
PostPosted: Wed Dec 19, 2007 11:09 am 
Newbie

Joined: Wed Dec 19, 2007 11:04 am
Posts: 10
I am developing an DB application using JPA where i need to update table having primitive type fields (Ex: int).

Sample Code:
***********

int salary = 200;
sql1 = "update exployee where sal > ?1";
Query query1 = em.createNativeQuery(sql1);
query1.setParameter(1, salary);

The setParameter is not accepting int since there is not method in Query class.

Also if i try to convert as integer object and try to update

query1.setParameter(1 , new Integer(salary));

I am getting run time error like follows..

[IntegerType] could not bind value '200' to parameter: 2; [BEA][SQLServer JDBC Driver]Invalid parameter binding(s).


Please help me to fix this.

Thanks,
Lakshmi


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 19, 2007 11:19 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
Seems you are using Java 1.4 then which doesn't support autoboxing of primitive types. Your alternative solution looks ok though. What is the type of the sal column in the database?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 19, 2007 11:21 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
Oh, and your update statement is missing the set clause. The statement as given is not valid SQL. May be that is the problem and the error message is misleading?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 19, 2007 11:50 am 
Newbie

Joined: Wed Dec 19, 2007 11:04 am
Posts: 10
Hi,

Thanks for the reply.
1. The actual Update Statment does have "set".

2. Table is in SQL Server and the salary column is 'int' type.
I cannot change the column type to String since it is replica of Production.

So creating Integer object and saving is also is not working with int.

I am using JDk1.6 and source version 1.6.


Thanks,

Lakshmi


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 19, 2007 3:22 pm 
Newbie

Joined: Wed Dec 19, 2007 11:04 am
Posts: 10
Thanks,

Finally i got it worked.


Lakshmi


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 20, 2007 9:23 pm 
Newbie

Joined: Mon Oct 29, 2007 2:33 pm
Posts: 16
Location: Cambridge, MA
The position number should be 0. setParameter(position, value) starts numbering from 0. It's better to use named parameters, so this kind of confusion doesn't happen. See for ex. JPwH, pg. 684.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.