java - In Vaadin how to display data from a joined table? -
i display products data on page using javaee
, vaadin
working except category column displays entire object instead of category name.
i joining 2 tables product
, category
based on category_id
column , fetching , displaying category name category
table. query mentioned in productdaoimpl
class below.
vaadin version in pom
<vaadin.version>7.6.5</vaadin.version>
productsui.java
@title("home") @theme("mytheme") @widgetset("com.study.crud.myappwidgetset") public class productsui extends ui { private static final logger log = logger.getlogger(productsui.class); private final grid productsgrid = new grid(); private datasource datasource; @override protected void init(vaadinrequest vaadinrequest) { getdatasource(vaadinrequest); configurecomponents(); buildlayout(); } private void getdatasource(vaadinrequest vaadinrequest) { vaadinservletrequest req = (vaadinservletrequest) vaadinrequest; this.datasource = (datasource) req.getservletcontext().getattribute("datasource"); } private void configurecomponents() { // products productdao productdao = new productdaoimpl(datasource); list<product> products = productdao.getproducts(); //set products in grid productsgrid.setcontainerdatasource(new beanitemcontainer<>(product.class, products)); productsgrid.setcolumnorder("productid", "name", "image", "listprice", "category", "active"); productsgrid.removecolumn("description"); productsgrid.removecolumn("createdby"); productsgrid.removecolumn("expirydate"); productsgrid.removecolumn("modifiedon"); } private void buildlayout() { verticallayout layout = new verticallayout(); //add products grid main layout layout.addcomponent(productsgrid); productsgrid.setsizefull(); layout.setsizefull(); layout.setmargin(true); layout.setspacing(true); setcontent(layout); } @webservlet(urlpatterns = "/*", name = "productsuiservlet", asyncsupported = true) @vaadinservletconfiguration(ui = productsui.class, productionmode = false) public static class productsuiservlet extends vaadinservlet { } }
product.java
public class product implements serializable { private int productid; private string name; private string description; private string image; private bigdecimal listprice; private category category; private date expirydate; private string createdby; private date modifiedon; private string active; public product() { } public product(int productid) { this.productid = productid; } public product(int productid, string name, string description, string image, bigdecimal listprice, category category, date expirydate, string createdby, date modifiedon, string active) { this.productid = productid; this.name = name; this.description = description; this.image = image; this.listprice = listprice; this.category = category; this.expirydate = expirydate; this.createdby = createdby; this.modifiedon = modifiedon; this.active = active; } public int getproductid() { return productid; } public void setproductid(int productid) { this.productid = productid; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getdescription() { return description; } public void setdescription(string description) { this.description = description; } public string getimage() { return image; } public void setimage(string image) { this.image = image; } public bigdecimal getlistprice() { return listprice; } public void setlistprice(bigdecimal listprice) { this.listprice = listprice; } public category getcategory() { return category; } public void setcategory(category category) { this.category = category; } public date getexpirydate() { return expirydate; } public void setexpirydate(date expirydate) { this.expirydate = expirydate; } public string getcreatedby() { return createdby; } public void setcreatedby(string createdby) { this.createdby = createdby; } public date getmodifiedon() { return modifiedon; } public void setmodifiedon(date modifiedon) { this.modifiedon = modifiedon; } public string getactive() { return active; } public void setactive(string active) { this.active = active; } @override public string tostring() { return "product{" + "productid=" + productid + ", name=" + name + ", description=" + description + ", image=" + image + ", listprice=" + listprice + ", category=" + category + ", expirydate=" + expirydate + ", createdby=" + createdby + ", modifiedon=" + modifiedon + ", active=" + active + '}'; } }
category.java
public class category implements serializable { private int categoryid; private string name; private string description; private date expirydate; private string createdby; private date modifiedon; private string active; public category() { } public category(int categoryid) { this.categoryid = categoryid; } public category(string name) { this.name = name; } public category(int categoryid, string name) { this.categoryid = categoryid; this.name = name; } public category(int categoryid, string name, string description, date expirydate, string createdby, date modifiedon, string active) { this.categoryid = categoryid; this.name = name; this.description = description; this.expirydate = expirydate; this.createdby = createdby; this.modifiedon = modifiedon; this.active = active; } public int getcategoryid() { return categoryid; } public void setcategoryid(int categoryid) { this.categoryid = categoryid; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getdescription() { return description; } public void setdescription(string description) { this.description = description; } public date getexpirydate() { return expirydate; } public void setexpirydate(date expirydate) { this.expirydate = expirydate; } public string getcreatedby() { return createdby; } public void setcreatedby(string createdby) { this.createdby = createdby; } public date getmodifiedon() { return modifiedon; } public void setmodifiedon(date modifiedon) { this.modifiedon = modifiedon; } public string getactive() { return active; } public void setactive(string active) { this.active = active; } @override public string tostring() { return "category{" + "categoryid=" + categoryid + ", name=" + name + ", description=" + description + ", expirydate=" + expirydate + ", createdby=" + createdby + ", modifiedon=" + modifiedon + ", active=" + active + '}'; } }
productdaoimpl.java
public class productdaoimpl implements productdao { private final datasource datasource; public productdaoimpl(datasource datasource) { this.datasource = datasource; } @override public list<product> getproducts() { string sql = "select a.*, b.name category_name product a, category b a.category_id = b.category_id"; log.debug(sql); statement stmt = null; resultset rs = null; connection cn = null; list<product> products = new arraylist<>(); try { cn = datasource.getconnection(); stmt = cn.createstatement(); rs = stmt.executequery(sql); while (rs != null && rs.next()) { int productid = new integer(stringutils.defaultstring(rs.getstring("product_id"))); string name = stringutils.defaultstring(rs.getstring("name")); string description = stringutils.defaultstring(rs.getstring("description")); string image = stringutils.defaultstring(rs.getstring("image")); bigdecimal listprice = new bigdecimal(stringutils.defaultstring(rs.getstring("list_price"))); int categoryid = new integer(stringutils.defaultstring(rs.getstring("category_id"))); string categoryname = stringutils.defaultstring(rs.getstring("category_name")); category category = new category(categoryid, categoryname); date expirydate = rs.getdate("expiry_date"); string createdby = stringutils.defaultstring(rs.getstring("created_by")); date modifiedon = rs.getdate("modified_on"); string active = stringutils.defaultstring(rs.getstring("active")); product product = new product(productid, name, description, image, listprice, category, expirydate, createdby, modifiedon, active); products.add(product); } log.debug("products = " + products); log.debug("products.size() = " + products.size()); return products; } catch (sqlexception | numberformatexception ex) { log.error("exception while getting products....", ex); return null; } //close resources { try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (cn != null) { cn.close(); } } catch (sqlexception ex) { log.error("exception while closing db resources rs, stmt or cn.......", ex); } { try { if (cn != null) { cn.close(); } } catch (sqlexception ex) { log.error("exception while closing cn.......", ex); } } } } }
the reason (since use beanitemcontainer
) vaadin calls every getter in product
class , tries cast every returned value string
*. when reaches category
getter calls tostring()
on returned value. since has overridden tostring()
method in category
, vaadin calls , displays returned value in grid
. should left tostring()
method untouched in category
class display value returned object.tostring()
.
the simple way fix particular problem add getter in product
class:
public string getcategoryname() { return category.getname(); }
and remove 'category' column.
*this not true when take consideration converters
columns
enough in scenario
Comments
Post a Comment