persistence icon indicating copy to clipboard operation
persistence copied to clipboard

explicitly-typed setParameter()

Open gavinking opened this issue 1 month ago • 1 comments

@beikov says people often have trouble with null arguments to parameters in native SQL queries.

We don't really have this problem with JPQL, since the parameters types are inferable from the query, or with stored procedures, since registerStoredProcedureParameter() takes a type. But we don't know how to infer types from native SQL.

[Well, in fact, it is possible to construct pathological cases where the type can't be inferred in JPQL, for example ?1 = ?2, but I doubt they're useful.]

Therefore, even though I'm still hoping that we eventually move away from the horrible name of this method, it is probably worth adding additional overloads.

  • <P> Query setParameter(String parameter, P value, Class<P> type)
  • <P> Query setParameter(String parameter, P value, Type<P> type)

The first one works for most cases. In particular, for simply adding a type to a null string or integer or whatever, it's just perfect.

The second overload is useful when things like AttributeConverters come into play. It lets you use an attribute type obtained from the static metamodel as the type of a parameter, letting you go beyond the basic types defined by the specification.

gavinking avatar Dec 09 '25 17:12 gavinking