Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between setParameter and set specific type parameters in hibernate

Tags:

java

hibernate

Hibernate's Query class has setters for any type of primitive parameter such as setString setBoolean setLong etc, but it also has a setParameter method which receives an Object and can replace all other setters.

Since they are not deprecated, is there any advantage to using specific type parameters in terms of performance? Should setParameter be used only with custom Objects?

like image 353
Alexandru Severin Avatar asked Jun 18 '26 10:06

Alexandru Severin


1 Answers

based on cursory read of the implementation of the Query class in here.

is there any advantage to using specific type parameters in terms of performance?

Yeah, as if you directly call the setParameter function, the hibernate will need to "guess" the type of the object. But, It looks like that those method (setString, setBoolean, etc) just a convenient method to set a parameter. Because, in the end, these function will call function setParamater(int,Object,Type). Which is, the same with the setParameter function.

Should setParameter be used only with custom Objects?

Not necessary. I mean, you still be able to use an Integer or the other wrapper class (Boolean, Float, etc). It just that, there is additional operation that hibernate needs to do to check what type the value is and handle it properly.

like image 117
kucing_terbang Avatar answered Jun 20 '26 23:06

kucing_terbang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!