I have a class MyObj. I want to use it for multiple tables. In the class
MyObj there is a field value whose type will be known at runtime, therefore
the type of this field is Object. I try to create two tables using the MyObj
class, but when an insert occurs in the second table, an exception occurs.
public class CreateTwoTablesWithDifferentSchemaTest extends
GridCommonAbstractTest {
/** Create two tables/caches with the same VALUE_TYPE*/
@Test
public void executeTest() throws Exception {
try (final Ignite server =
Ignition.start(Config.getServerConfiguration());
final IgniteClient client = Ignition.startClient(new
ClientConfiguration()
.setAddresses(Config.SERVER)
.setBinaryConfiguration(new
BinaryConfiguration().setCompactFooter(true)))) {
createTableAndInsert(client, "MyTable1", "int");
createTableAndInsert(client, "MyTable2", "varchar");
}
}
private void createTableAndInsert(IgniteClient client, String tableName,
String typeField) {
/**
* Can use tableName instead of MyObj.class.getName(), but then you
can’t use the {@link ClientCache#get(Object)} method.
* */
client.query(new SqlFieldsQuery("CREATE TABLE IF NOT EXISTS " +
tableName + " (id int primary key, value " + typeField + ") " +
"WITH \"VALUE_TYPE=" + MyObj.class.getName() + ",CACHE_NAME=" +
tableName + ",atomicity=transactional,template=partitioned\"")
).getAll();
final Object value = "int".equals(typeField) ? 1 : "value_1";
try {
client.query(new SqlFieldsQuery("INSERT INTO " + tableName + "
(id, value) values(?,?)")
.setArgs(1, value)
).getAll();
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
static class MyObj {
private Integer id;
//It is assumed that the type of this property will depend on the
type of field in the table.
private Object value;
public MyObj(Integer id, Object value) {
this.id = id;
this.value = value;
}
}
}
Can I somehow solve this problem?
--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/