Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combination of @Define and @BindBean in same method signature causes "Invalid Column Index" exception #2508

Open
DebajitKumarPhukan opened this issue Oct 5, 2023 · 3 comments
Assignees
Labels
investigation needed testing waiting-for-op Waiting for a response from the original poster

Comments

@DebajitKumarPhukan
Copy link

DebajitKumarPhukan commented Oct 5, 2023

Problem Statement:
I wanted to dynamically update the table name using @Define("table") and bind the named parameters using @BindBean for a query but in my attempt to do so resulted in "Invalid Column Index" exception. When I hardcoded the table name and removed @Define("table"), it worked as expected.

Jdbi BOM Version:
3.41.3

Example:

@SqlBatch("update <table> set name = :name where id = :id")
void updateAll(@Define("table") String table, @BindBean Iterable<User> users);

User Class:

public Class user {

private String name;
private Integer id;

public String getName(){
 return this.name;
}
public Integer getId(){
  return this.id;
}
public void setName(String name){
  this.name = name;
}
public void setId(Integer id){
  this.id = id;
}
}

Table Name:
user

Table Schema (Oracle):

name varchar2(255)
id number(30)
@hgschmie
Copy link
Contributor

I assume it is void updateAll(@Define("table") String table, @BindBean Iterable<User> users); in the example.

@hgschmie
Copy link
Contributor

So this works fine:

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jdbi.v3.sqlobject;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import org.jdbi.v3.core.Something;
import org.jdbi.v3.sqlobject.customizer.BindBean;
import org.jdbi.v3.sqlobject.customizer.Define;
import org.jdbi.v3.sqlobject.statement.SqlBatch;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.testing.junit5.JdbiExtension;
import org.jdbi.v3.testing.junit5.internal.TestingInitializers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.assertj.core.api.Assertions.assertThat;

public class TestSqlBatch {
    @RegisterExtension
    public JdbiExtension h2Extension = JdbiExtension.h2().withPlugin(new SqlObjectPlugin()).withInitializer(TestingInitializers.something());

    @Test
    public void testIssue2508() {
        var data = new ArrayList<Something>();

        for (int i = 0; i < 10; i++) {
            h2Extension.getSharedHandle().execute("INSERT INTO something (id, name) VALUES (?, ?)", i, String.valueOf(i));

            Something something = new Something(i, UUID.randomUUID().toString());
            data.add(something);
        }

        h2Extension.getJdbi().useExtension(Dao.class, d -> d.updateAll("something", data));

        List<String> result = h2Extension.getJdbi().withExtension(Dao.class, d -> d.getNames("something"));

        List<String> names = data.stream()
            .map(Something::getName)
            .collect(Collectors.toList());

        assertThat(result).containsAll(names);
    }

    public interface Dao {
        @SqlBatch("update <table> set name = :name where id = :id")
        void updateAll(@Define("table") String tableName, @BindBean Iterable<Something> data);

        @SqlQuery("select name from <table> order by id")
        List<String> getNames(@Define("table") String table);
    }

}

Not sure where to go from here. We probably need to see some stack traces and a bit more of the code that is calling the dao.

@hgschmie hgschmie added testing investigation needed waiting-for-op Waiting for a response from the original poster labels Oct 24, 2023
@hgschmie hgschmie self-assigned this Oct 24, 2023
@DebajitKumarPhukan
Copy link
Author

@hgschmie I was invoking the Dao using jdbi.onDemand(), can you try with the same approach (as shown in below example) instead of h2Extension.getJdbi() ?

Dao dao = jdbi.onDemand(Dao.class);
dao.updateAll("mytable", data);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigation needed testing waiting-for-op Waiting for a response from the original poster
Development

No branches or pull requests

2 participants