I’d like to use an integer value in one of my containers as a rowid, so I can use getContainerProperty() to get properties from another container.
Currently my code iterates through all the rows in container1, then gets a property from each row called “container2ID”, I want to use this as a RowId to query Container2.
From the looks of things, I can’t cast from Integer (which “container2ID” is stored as) to a RowId. Any suggestions?
//iterate through each row in the container1
for (Iterator i = container1.getItemIds().iterator(); i.hasNext();)
{
//get the rowid
Object iid = i.next();
//use rowid to get the "container2ID"
Property container2RowId = container1.getContainerProperty(iid, "container2ID"); //WORKS UP TO HERE
//use "container2ID" on the container2 to get "name"
Property n = container2.getContainerProperty(container2RowId, "name");
}
You didn’t mention what container type you use, exactly.
You can use any integer as the item ID at least in IndexedContainer, as well as in some other containers. But not in all containers, for example BeanItemContainer uses the bean object itself as the item ID.
In your code, you might want to use container2RowId.getValue() on line 11, instead of the property object. The property object is most probably an invalid item ID - you want to use its value.
Thanks for your reply. I’m using an SQLContainer and working in 6.8.2. Does that change anything? Would an IndexedContainer be better?
I’ll give your suggestion a go tomorrow, although I get the feeling I’ve tried to cast it (or just used .getValue()) to an Integer before and it hasn’t worked. I’ll give it a go and let you know.
I had a go at the .getValue() solution and it didn’t work, I also double checked by putting an integer in the first parameter of .getContainerProperty() and that didn’t work. From the looks of things, if you’re using an SQLContainer it’s a RowId object or nothing.
I’m not entirely sure where to go from here, from what I’ve read in the book of vaadin it seems SQLContainer is the only container which will take data from my Postgres database?
Ok, so you’re using SQLContainer. You are right, they don’t take integers as item IDs, but a RowId object, which contains an array of the primary keys. Maybe something like “new RowId(new Object{container2RowId.getValue()})” would work for the itemId parameter? (at line 11)
Maybe the addReference() would do what you want? I haven’t looked into how it works, exactly.
Your case could perhaps be solved also by a JOIN query, but it might not work so easily if you need write access. I’m not immediately sure how that works either (I didn’t write the SQLContainer chapter).
You can also use at least JPAContainer for database access, and I would think JPA works with Postgres.
I had solved the issue after I posted earlier with exactly the method you suggested, creating a RowId from an array of Object. Not the cleanest method, but it works!
Object[] o = new Object[1]
;
o[0]
= container2RowId.getValue();
RowId iid = new RowId(o);
I had also previously tried addReference() and using a JOIN (nice to know that I was thinking along the right lines!) but couldn’t get them running.