{"record":{"id":"f14836060e08ffd4","repo":"hibernate/hibernate-orm","slug":"cannot-mix-parameter-styles-between-jdbc-style-or","errorCode":null,"errorMessage":"Cannot mix parameter styles between JDBC-style, ordinal and named in the same query","messagePattern":"Cannot mix parameter styles between JDBC-style, ordinal and named in the same query","errorType":"exception","errorClass":"ParameterRecognitionException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sql/internal/ParameterRecognizerImpl.java","lineNumber":100,"sourceCode":"\n\tpublic ArrayList<ParameterOccurrence> getParameterList() {\n\t\treturn parameterList;\n\t}\n\n\tpublic String getAdjustedSqlString() {\n\t\treturn sqlStringBuffer.toString();\n\t}\n\n\t// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\t// Recognition code\n\n\t@Override\n\tpublic void ordinalParameter(int sourcePosition) {\n\t\tif ( parameterStyle == null ) {\n\t\t\tparameterStyle = ParameterStyle.JDBC;\n\t\t}\n\t\telse if ( parameterStyle != ParameterStyle.JDBC ) {\n\t\t\tthrow new ParameterRecognitionException( \"Cannot mix parameter styles between JDBC-style, ordinal and named in the same query\" );\n\t\t}\n\n\t\tint implicitPosition = ordinalParameterImplicitPosition++;\n\n\t\tQueryParameterImplementor<?> parameter = null;\n\n\t\tif ( positionalQueryParameters == null ) {\n\t\t\tpositionalQueryParameters = new HashMap<>();\n\t\t}\n\t\telse {\n\t\t\tparameter = positionalQueryParameters.get( implicitPosition );\n\t\t}\n\n\t\tif ( parameter == null ) {\n\t\t\tparameter = QueryParameterPositionalImpl.fromNativeQuery( implicitPosition );\n\t\t\tpositionalQueryParameters.put( implicitPosition, parameter );\n\t\t}\n","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sql/internal/ParameterRecognizerImpl.java#L82-L118","documentation":"ParameterRecognizerImpl enforces one parameter style per query: a bare JDBC '?' sets ParameterStyle.JDBC. When a bare '?' is recognized after a named (':name') or JPA-ordinal ('?n') parameter was already seen, parameterStyle is NAMED and this ParameterRecognitionException is thrown. Mixing styles would make bind positions ambiguous, so Hibernate rejects it up front.","triggerScenarios":"A query string combining ':name' or '?1' with a bare '?', e.g. \"... where x = :val and y = ?\". The bare '?' can also come from literal SQL such as PostgreSQL JSONB operators (? ?| ?&) which the parser cannot distinguish from placeholders. Thrown at query creation during ParameterRecognizer callbacks.","commonSituations":"PostgreSQL JSONB queries using the ? / ?| / ?& operators alongside real named parameters; copy-pasted SQL fragments in different styles; partial migration of a query from '?' to ':name'.","solutions":["Use one style consistently: convert the bare '?' into a named parameter (':y') or convert everything to JPA ordinals (?1, ?2).","For literal '?' operators in PostgreSQL JSONB SQL, replace them with the equivalent functions (jsonb_exists, jsonb_exists_any, jsonb_exists_all) or set hibernate.query.native.ignore_jdbc_parameters=true so bare '?' is not treated as a parameter.","Escape/inline literal question marks through dialect-safe function calls instead of raw operators."],"exampleFix":"-- before\nselect * from person where x = :val and y = ?\n\n-- after\nselect * from person where x = :val and y = :y","handlingStrategy":"validation","validationCode":"static void validateSingleParameterStyle(String sql) {\n    String scrubbed = sql.replaceAll(\"'([^']*)'\", \"?\").replaceAll(\"\\\"([^\\\"]*)\\\"\", \"?\"); // ignore literals\n    var m = java.util.regex.Pattern.compile(\"\\\\?(\\\\d+)\\\\b|:(\\\\w+)|\\\\?\").matcher(scrubbed);\n    boolean jdbc = false, namedOrOrdinal = false;\n    while (m.find()) {\n        if (m.group(1) != null || m.group(2) != null) namedOrOrdinal = true;\n        else jdbc = true;\n    }\n    if (jdbc && namedOrOrdinal) throw new IllegalArgumentException(\"Query mixes bare '?' with ':name'/'?n' parameters\");\n}","typeGuard":null,"tryCatchPattern":"try { session.createNativeQuery(sql); } catch (org.hibernate.query.ParameterRecognitionException e) { /* normalize all parameters to one style and rebuild the query */ throw e; }","preventionTips":["Pick one parameter style per codebase module and enforce it in review/lint.","For PostgreSQL JSONB '?'-operators in native SQL, set hibernate.query.native.ignore_jdbc_parameters=true or use jsonb_exists/jsonb_exists_any functions.","Run validateSingleParameterStyle in tests over native SQL constants."],"tags":["hibernate","native-query","parameter-binding","parameter-recognition-exception","postgresql-jsonb"],"backgroundTag":"mixed-query-parameter-styles","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}