Vaadin How to deal with nested properties

First off all we need to know what is called nested Properties in Vaadin. Defition from Vaadin Doc: 
"If you have a nested bean with an 1:1 relationship inside a bean type contained in a BeanContainer or BeanItemContainer, you can add its properties to the container by specifying them with addNestedContainerProperty(). The feature is defined at the level of AbstractBeanContainer.  As with the bean in a bean container, also a nested bean must have public visibility or otherwise an access exception is thrown. An intermediate reference from a bean in the bean container to a nested bean may have a null value"

Consider 2 classes Etudiant.java and DDLSexe.java

Etudiant.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Etudiant", propOrder = {
    "id",
    "matricule",
    "nom",
    "prenom",
    "ddlSexe"
})
@Entity(name = "Etudiant")
@Table(name = "ETUDIANT")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Etudiant implements Serializable{

    private final static long serialVersionUID = 1L;
    @XmlElement(required = true)
    protected BigInteger id;
    @XmlElement(required = true)
    protected String matricule;
    @XmlElement(required = true)
    protected String nom;
    @XmlElement(required = true)
    protected String prenom;
    @XmlElement(required = true)
    protected DDLSexe ddlSexe;
// getters and setters
}

 DDLSexe.java

XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DDLSexe", propOrder = {
    "id",
    "code",
    "nom",
    "langueId"
})
@ReadOnly
@Entity(name = "DDLSexe")
@Table(name = "V_SEXE")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class DDLSexe implements Serializable{

    private final static long serialVersionUID = 1L;
    protected BigInteger id;
    protected String code;
    protected String nom;
    protected BigInteger langueId;
// getters and setters
}

Using  Nested Properties within a Table or Grid

In our example DDLSexe is a nested class. Now if we want to show  Etudiant object in a table or grid the we have

EtudiantGrid.java

public class EtudiantGrid extends Grid {

	public EtudiantGrid() {
		setSizeFull();
		setSelectionMode(SelectionMode.SINGLE);

		BeanItemContainer<Etudiant> container = new BeanItemContainer<Etudiant>(
				Etudiant.class);
		setContainerDataSource(container);
		container.addNestedContainerProperty("ddlSexe.nom");
		
		removeAllColumns();
		addColumn("nom");
		addColumn("prenom");
		addColumn("matricule");
		addColumn("ddlSexe.nom");
	}

	private BeanItemContainer<Etudiant> getContainer() {
		return (BeanItemContainer<Etudiant>) super.getContainerDataSource();
	}

	public void setEtudiants(Collection<Etudiant> etudiants) {
		getContainer().removeAllItems();
		getContainer().addAll(etudiants);
	}
}

 

We could also override the header of a nested field by using:

getColumn("ddlSexe.nom").setHeaderCaption("Genre");

Enregistrez vous aujoud'hui, achetez et vendre en ligne gratuitement Clickez ici