setParameter与hibernate中设置特定类型参数的区别

时间:2015-10-06 07:54:20

标签: java hibernate

Hibernate的Query类具有任何类型的原始参数的设置器,例如exec GetResults 'ProjectID', 'ProjectList', 10, 20, 'ProjectStatus', '5', 'ProjectName', 'ASC' setString setBoolean等,但它也有一个setLong方法接收setParameter并且可以替换所有其他的设置者。

由于它们不被弃用,在性能方面使用特定类型参数是否有任何优势? Object是否应仅用于自定义对象?

1 个答案:

答案 0 :(得分:6)

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.