Form Issue

Dears,

I want to create a form where 1To1relationship exists and fields are presented as displayed below.
Once the user fills out the form, it is submitted to be saved but I am getting an error “id_address” is null error.
There are 2 tables. Provider and ProviderAddress. There is 1 to 1 relationship between them with the foreignkey “id_address” in provider table.


I am gettting “Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column ‘id_address’ cannot be null” error when the form is being saved.

@SuppressWarnings(“serial”)
public class ProviderEditor extends Window implements Button.ClickListener,
FormFieldFactory {

private Item providerItem;
private Form editorForm;
private Button saveButton;
private Button cancelButton;

public ProviderEditor(Item providerItem) {
    this.providerItem = providerItem;
    editorForm = new Form();


// These are the fields of the Provider object

editorForm.setItemDataSource(providerItem, Arrays.asList(“name”,“openAt”, “closeAt”, “email”, “phone”, “commission”,“idAddress”));
saveButton = new Button(“Save”, this);
cancelButton = new Button(“Cancel”, this);


// These are the fields of the Provider Address object.

VerticalLayout layout = new VerticalLayout();
TextField address1 = new TextField();
address1.setCaption(“Address1”);
editorForm.addField(“Address1”, address1);

    TextField address2 = new TextField();
    address2.setCaption("Address2");
    editorForm.addField("Address2", address2);
    
    JPAContainer<Area> areas = JPAContainerFactory.make(Area.class, AdminUI.PERSISTENCE_UNIT);
    final Select areaSelect = new Select("Select an Area", areas);
    areaSelect.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
    areaSelect.setItemCaptionPropertyId("name");
    editorForm.addField("Area", areaSelect);
    
    editorForm.getFooter().addComponent(saveButton);
    editorForm.getFooter().addComponent(cancelButton);
}

 /*
 * (non-Javadoc)
 * 
 * @see com.vaadin.ui.Button.ClickListener#buttonClick(com.vaadin.ui.Button.
 * ClickEvent)
 */
@Override
public void buttonClick(Button.ClickEvent event) {
    if (event.getButton() == saveButton) {
        Field address1= editorForm.getField("Address1");
        Field address2= editorForm.getField("Address2");
        Field area= editorForm.getField("Area");


// Saving ProviderAddress db record

EntityManager em = Persistence.createEntityManagerFactory(“allhealthy”).createEntityManager();
em.getTransaction().begin();
Area selectedArea= em.find(Area.class, area.getValue());
ProviderAddress providerAddress= new ProviderAddress();
providerAddress.setIdArea(selectedArea);
providerAddress.setAddress1(address1.getValue().toString());
providerAddress.setAddress2(address2.getValue().toString());
providerAddress.setLabel(“Home”);

        em.persist(providerAddress);
        em.getTransaction().commit();


// Setting the ProviderAddress to the Provider’s datasource
//‘idAddress’ represents ‘id_address’ in the Provider Hibernate Mapping class.

editorForm.getItemDataSource().getItemProperty(“idAddress”).setValue(providerAddress);


editorForm.commit(); // At this stage I am getting id_address null error.

fireEvent(new EditorSavedEvent(this, providerItem));
} else if (event.getButton() == cancelButton) {
editorForm.discard();
}
close();
}

/*
 * (non-Javadoc)
 * 
 * @see com.vaadin.ui.FormFieldFactory#createField(com.vaadin.data.Item,
 * java.lang.Object, com.vaadin.ui.Component)
 */
@Override
public Field createField(Item item, Object propertyId, Component uiContext) {
    Field field = DefaultFieldFactory.get().createField(item, propertyId, uiContext);
   
    if (field instanceof TextField) {
        ((TextField) field).setNullRepresentation("");
    }

    field.addValidator(new BeanValidator(Provider.class, propertyId
            .toString()));

    if (propertyId.equals("idAddress")){
        field.setVisible(false);
    }
    return field;
}