1

I have Java code:

String updateSql = "UPDATE table_name SET field_two = :field_two"
                + " WHERE field_one = :field_one AND field_two <> :field_two"; 
handle.createUpdate(updateSql)
    .bindBean(myBean)
    .execute();
@Data
public class MyBean() {
   private String fieldOne;
   private String fieldTwo;
}

When Jdbi tries to bind the field_two the second time, it throws UnableToCreateStatementException: Missing named parameter field_two in binding.

How can I bind the field_two appeared multiple times in the query using bindBean()?

EzyHoo
  • 301
  • 2
  • 14
  • 2
    Does it work if you use `:fieldOne` and `:fieldTwo` as parameters in the query? – Henning Feb 14 '23 at 07:59
  • @Henning Not working either. The second field_two binding will throw UnableToCreateStatementException – EzyHoo Feb 14 '23 at 16:28
  • 1
    If I use the suggestion made by @Henning I cannot recreate your problem. The named parameters need to match the Java field names - not the database table column names. The fact that you get `UnableToCreateStatementException` when you use the (apparently) correct field names suggests a problem somewhere else, not shown in your question. – andrewJames Feb 14 '23 at 20:53
  • @andrewJames Yes. That was a silly mistake. The field name was not matching up, I was using the column name which is wrong. Please add an answer! :D – EzyHoo Feb 15 '23 at 06:43
  • 1
    Glad you solved it. You are welcome to [write your own answer](https://stackoverflow.com/help/self-answer) for this. That way, you can explain exactly how you fixed the issue, for future visitors. – andrewJames Feb 15 '23 at 13:07

1 Answers1

0

It turns out that the binding needs to have the same name with the field name:

String updateSql = "UPDATE table_name SET field_two = :fieldTwo"
                + " WHERE field_one = :fieldOne AND field_two <> :fieldTwo";
EzyHoo
  • 301
  • 2
  • 14