vaadin + webservices

hi,

first of all congrats for this excellent framework that is vaadin.
now, i’m trying to use vaadin & webservices do interact with my database. the webservices are using hibernate. take this pojo as anm example:


@Entity
@Table(name = "WORKSTATION", schema = "APLISCANDATA", uniqueConstraints =
@UniqueConstraint(columnNames = {"NAME", "IDWORKSTATION_TYPE"}))
public class Workstation implements java.io.Serializable {

    private int idworkstation;
    private WorkstationType workstationType;
    private String name;
    private String description;
    private boolean active;
    private Date addedwhen;
    private String addedby;
    private boolean refreshData;
    private String swversion;

    public Workstation() {
    }

    public Workstation(int idworkstation, WorkstationType workstationType, String name, Date addedwhen, String addedby, boolean refreshData, String swversion) {
        this.idworkstation = idworkstation;
        this.workstationType = workstationType;
        this.name = name;
        this.addedwhen = addedwhen;
        this.addedby = addedby;
        this.refreshData = refreshData;
        this.swversion = swversion;
    }

    @Id
    @Column(name = "IDWORKSTATION", unique = true, nullable = false, precision = 5, scale = 0)
    public int getIdworkstation() {
        return this.idworkstation;
    }

    public void setIdworkstation(int idworkstation) {
        this.idworkstation = idworkstation;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "IDWORKSTATION_TYPE", nullable = false)
    public WorkstationType getWorkstationType() {
        return this.workstationType;
    }

    public void setWorkstationType(WorkstationType workstationType) {
        this.workstationType = workstationType;
    }

    @Column(name = "NAME", nullable = false, length = 20)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "DESCRIPTION", length = 200)
    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Column(name = "ACTIVE", precision = 1, scale = 0)
    public boolean getActive() {
        return this.active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "ADDEDWHEN", nullable = false, length = 7)
    public Date getAddedwhen() {
        return this.addedwhen;
    }

    public void setAddedwhen(Date addedwhen) {
        this.addedwhen = addedwhen;
    }

    @Column(name = "ADDEDBY", nullable = false, length = 20)
    public String getAddedby() {
        return this.addedby;
    }

    public void setAddedby(String addedby) {
        this.addedby = addedby;
    }

    @Column(name = "REFRESH_DATA", nullable = false, precision = 1, scale = 0)
    public boolean isRefreshData() {
        return this.refreshData;
    }

    public void setRefreshData(boolean refreshData) {
        this.refreshData = refreshData;
    }

    @Column(name = "SWVERSION", nullable = false, length = 20)
    public String getSwversion() {
        return this.swversion;
    }

    public void setSwversion(String swversion) {
        this.swversion = swversion;
    }

}

note that the field
workstationType
referes to this pojo:


@Entity
@Table(name = "WORKSTATION_TYPE", schema = "APLISCANDATA")
public class WorkstationType implements java.io.Serializable {

    private int idworkstationType;
    private String description;

    public WorkstationType() {
    }

    public WorkstationType(int idworkstationType, String description) {
        this.idworkstationType = idworkstationType;
        this.description = description;
    }

    @Id
    @Column(name = "IDWORKSTATION_TYPE", unique = true, nullable = false, precision = 5, scale = 0)
    public int getIdworkstationType() {
        return this.idworkstationType;
    }

    public void setIdworkstationType(int idworkstationType) {
        this.idworkstationType = idworkstationType;
    }

    @Column(name = "DESCRIPTION", nullable = false, length = 20)
    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

now, i whant to have a vaadin form to edit a pojo from type
Workstation
witch has an attribute from type
WorkstationType
, so this attribute could be a combobox with a beanitemcontainer as a datasource, like this:


cbWORKSTATIONTYPES = new ComboBox("Type");
cbWORKSTATIONTYPES.setWidth(COMMON_FIELD_WIDTH);
cbWORKSTATIONTYPES.setImmediate(true);

BeanItemContainer<WorkstationType> mContainer = new BeanItemContainer<WorkstationType>(WorkstationType.class);

for (WorkstationType bussItem : mWorkstationTypesList) {  //mWorkstationTypesList is the list from my webservice witch is a WorkstationType List
     mContainer.addBean(bussItem);
}

cbWORKSTATIONTYPES.setContainerDataSource(mContainer));
cbWORKSTATIONTYPES.setItemCaptionPropertyId("description");
cbWORKSTATIONTYPES.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
cbWORKSTATIONTYPES.setFilteringMode(Select.FILTERINGMODE_STARTSWITH);
cbWORKSTATIONTYPES.setRequired(true);
cbWORKSTATIONTYPES.setNullSelectionAllowed(false);

what happens is, when i make the form databind from my pojo, my combobox does not get selected with the value that the pojo has; it stays empty. if i select a value from the combo and do the commit (and then the following database update) the value from the combobox is correct.

seems to me that the combo has the right value after the form.databind but the combo doesn’t “draw it…”

can someone help me figure this out?

thansk

Hi,

The problem is probably that the instance of WorkstationType referred to from Workstation is not the same instance as in the combo box. This is a common problem when working with Hibernate, since Hibernate loads new instances of the same objects.

The solution is to implement proper equals() and hashCode() methods in your WorkstationType class.

HTH,
/Jonatan

Or use a BeanContainer (new in Vaadin 6.5) that permits the item identifier to be different from the item. You could use setBeanIdProperty() to use the database id as the item id - or implement your own BeanIdResolver or give the ids explicitly.