How to create a generated column using foreign key object

This has to be simple and I just don’t see exactly how to do it…

I have a class definition which contains a foreign key reference (category_id == Bar.id)

class Foo{
private Integer id;
private String description;

@ManyToOne
@JoinColumn(name="category_id")
private Bar myCategory;

};

class Bar{
private Integer id;
private String category;
};

I can load the Foo objects and show them in a table, but I would like to add a column to show the category string from class Bar. How do I define
the generated column?

My table definition looks like this - I’m using a JPAContainer as the data source

tbl_Devices = new Table();
tbl_Devices.setSelectable(true);
tbl_Devices.setImmediate(true);

jc_TRP = app.getDbMgr().jcGetAllTRP(false);   // Load the Foo objects

tbl_Devices.setContainerDataSource(jc_TRP);

 // Define the generated column here - how???

tbl_Devices.setVisibleColumns(new Object[] { "id", "description", "????? what goes here ????" });

Thanks very much,

nbc

Somewhat like this (written without an editor, so may not compile)


table.addGeneratedColumn("yourColumnName",  new Table.ColumnGenerator() {

    public Component generateCell(Table source, Object itemId,
                                  Object columnId) {
        Bar bar = (Bar) source.getItemProperty("myCategory").getValue();
        Label label = new Label(bar.getCategory());
        return label;
    }
});

Take a look at the
Book of Vaadin
for more details.

Thanks Kim - that helped. In fact, the code I had to write is a bit different from yours - yours did not compile for me - mine looks like this:

               tbl_Devices.addGeneratedColumn("Category", new Table.ColumnGenerator() {
		private static final long serialVersionUID = 1L;

		@Override
		public Component generateCell(Table source, Object itemId, Object columnId) {
			Label label = new Label("NULL");
			Object oid = null;

			oid = source.getItem(itemId).getItemProperty("myCategory.id").getValue();   // This gives me an Integer key for the Category
			if(jc_TRC.getItem(oid) != null){                                 // Get the Category object out of the JPAContainer
				Category c = jc_TRC.getItem(oid).getEntity();
				label = new Label(c.getCategory());                // Get the Category name
			}
			return(label);
		}
	});

One question left - I can see that the itemId parameter identifies the row in the table that is being processed - but I can’t see a use for the columnId parameter - at least not in this case. Under what circumstances would the columnId parameter be used?

Thanks again for the help

nbc

You need the column id if you have two generated columns which both use the same ColumnGenerator. With the column id you can identify which generated column you are processing.

I see - thanks very much…

nbc

Can anyone explain where I get wrong because I have exception “Caused by: java.lang.IllegalArgumentException: Can not add the same GeneratedColumn twice, id:partners”

public class Companies {

// Fields

private CompaniesId id;
private Partners partners;
private String dsc;
private Date dtc;
private Date dtm;
private String usc;
private String usm;
private BigDecimal no1;
    private Set<OrgUnit> orgUnits = new HashSet<OrgUnit>(0);

}

com.vaadin.ui.Table table = new Table…;
BeanItemContainer beans = new BeanItemContainer(Companies.class);
beans.addAll(listOFCompanies);
table.setContainerDataSource(beans);
String removeColumns = {“id”,“orgUnits”,“dtc”,“dtm”,“usc”,“usm”,“no1”};
for (String pom : removeColumns) {
beans.removeContainerProperty(pom);
}
table.addGeneratedColumn(“partneri”, new ColumnGenerator() {

public Object generateCell(Table table, Object itemId, Object columnId) {

Item item = table.getItem(itemId);

            Partners par = (Partners) item.getItemProperty("partners").getValue();
            return new Label(par.getDsc()); 

}
});
table.setVisibleColumns(new Object{“dsc”,“partneri”});
table.setColumnWidth(“dsc”, 150);
table.setColumnHeader(“partneri”, “Partner”);
table.setColumnHeader(“dsc”, “Ime Kompanije”);
table.setSelectable(true);

Sorry my mistake this is ok code. I call it twice in row.