Example 1: Instantiate a context with an inner bean collection and assign a value to one of the inner collection's elements.
<bColl id="customerBean" bean="com.ibm.btt.bean.test.Customer"/>
<context id="customerCtx" type="test"><refKColl refId="customerBean"/></context>
Context ctx = ContextFactory.createContext("CustomerCtx"); ctx.setValueAt("name", "Steven"); ctx.trySetValueAt("name", "Steven");
Context's trySetValueAt() method works differently from setValueAt(). When the composite key points to an element that does not exist, setValueAt() throws exception , while trySetValueAt() returns -1 and does not throw any exception.
Example 2: Instantiate a bean collection with an inner bean collection defined in the Java™ Bean and assign a value to one of the inner collection's elements.
<bColl id="addressBean" bean="com.ibm.btt.bean.test.Address"/> <bColl id="customerBean" bean="com.ibm.btt.bean.test.Customer"/>
The Address object is an attribute of the Customer object.
Context ctx = ContextFactory.createContext("CustomerCtx"); ctx.setValueAt("address.city","Beijing"); ctx.trySetValueAt("address.city","Beijing");
Example 3: Instantiate a bean collection with an inner bean collection defined in the Java Bean, which has an ArraryList attribute and a HashMap attribute, and assign a value to one of the inner collection's elements.
<bColl id="complexBean" bean="com.ibm.btt.bean.test.ComplexJavaBean"/>
HashMap hashmap1 = new HashMap(); ArrayList arraylist1 = new ArrayList(); byte byte1[] = new byte[5]; arraylist1.add(byte1); hashmap1.put("key1", arraylist1); Context ctx = ContextFactory.createContext("ComplexCtx"); ctx.setValueAt("hmp", hashmap1); ctx.setValueAt("hmp.key1.0.4", (byte)1); ctx.trySetValueAt("hmp.key1.0.3", (byte)2);
The HashMap contains an ArraryList object, which contains a byte array and a Customer object. Using the composite key hmp.key1.0.4 or hmp.key1.1.name, you can set value to any subelement of this complex data structure.