You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
The text was updated successfully, but these errors were encountered:
/* * 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. */packageorg.jdbi.v3.sqlobject;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.UUID;
importjava.util.stream.Collectors;
importorg.jdbi.v3.core.Something;
importorg.jdbi.v3.sqlobject.customizer.BindBean;
importorg.jdbi.v3.sqlobject.customizer.Define;
importorg.jdbi.v3.sqlobject.statement.SqlBatch;
importorg.jdbi.v3.sqlobject.statement.SqlQuery;
importorg.jdbi.v3.testing.junit5.JdbiExtension;
importorg.jdbi.v3.testing.junit5.internal.TestingInitializers;
importorg.junit.jupiter.api.Test;
importorg.junit.jupiter.api.extension.RegisterExtension;
importstaticorg.assertj.core.api.Assertions.assertThat;
publicclassTestSqlBatch {
@RegisterExtensionpublicJdbiExtensionh2Extension = JdbiExtension.h2().withPlugin(newSqlObjectPlugin()).withInitializer(TestingInitializers.something());
@TestpublicvoidtestIssue2508() {
vardata = newArrayList<Something>();
for (inti = 0; i < 10; i++) {
h2Extension.getSharedHandle().execute("INSERT INTO something (id, name) VALUES (?, ?)", i, String.valueOf(i));
Somethingsomething = newSomething(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);
}
publicinterfaceDao {
@SqlBatch("update <table> set name = :name where id = :id")
voidupdateAll(@Define("table") StringtableName, @BindBeanIterable<Something> data);
@SqlQuery("select name from <table> order by id")
List<String> getNames(@Define("table") Stringtable);
}
}
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 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);
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:
User Class:
Table Name:
user
Table Schema (Oracle):
The text was updated successfully, but these errors were encountered: