2016-04-21 12 views
0

Ich zeige alle Produktdaten auf einer Seite unter Verwendung JavaEE und Vaadin an, die außer der Kategorie-Spalte arbeitet, die das gesamte Objekt statt nur des Kategorie-Namens anzeigt.In Vaadin, wie Daten von einer verbundenen Tabelle angezeigt werden?

ich zwei Tabellen product und category basierend auf category_id Säule und das Abrufen und Anzeigen der Kategorienamen aus category Tabelle am Beitritt. Abfrage ist in ProductDaoImpl Klasse unten erwähnt.

Products In Vaadin

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() { 
     // get all products 
     ProductDao productDao = new ProductDaoImpl(dataSource); 
     List<Product> products = productDao.getProducts(); 

     //set all the 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 to the 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 as category_name from product a, category b where 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 
     finally { 
      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); 
      } finally { 
       try { 
        if (cn != null) { 
         cn.close(); 
        } 
       } catch (SQLException ex) { 
        LOG.error("Exception while closing cn.......", ex); 
       } 
      } 
     } 
    } 
} 

Antwort

1

Der Grund dafür ist, dass (da Sie BeanItemContainer verwenden) Vaadin jedes Getter in Ihrer Product Klasse aufruft und versucht, jeden zurückgegebenen Wert an String * zu übergeben. Wenn es den Category Getter erreicht, ruft es toString() auf dem zurückgegebenen Wert auf. Da Sie die Methode toString() in einer Category überschrieben haben, ruft Vaadin sie auf und zeigt den zurückgegebenen Wert in Grid an. Sollten Sie die Methode toString() in der Klasse Category unverändert lassen, wird der von Object.toString() zurückgegebene Wert angezeigt.

Der einfache Weg, um dieses besondere Problem zu beheben, würde diese Getter in Ihrer Product Klasse hinzuzufügen sein:

public String getCategoryName() { 
    return category.getName(); 
} 

und die ‚Kategorie‘ Spalte zu entfernen.

* ist dies nicht immer der Fall, wenn wir in Betracht ziehen Converters für die Columns aber es ist gut genug, um in diesem Szenario

+0

Dank! Das hat funktioniert :) – user2325154

Verwandte Themen