tstyle.org/cayenne/">Cayenne is an Object Relational Mapping (ORM) framework. The CayenneForm supports creating (inserting) and saving (updating) Cayenne {@link DataObject} instances. This form willautomatically apply the given data objects required and max length validation constraints to the form fields.
The CayenneForm uses the thread local
DataContext obtained via
DataContext.getThreadDataContext() for all object for persistence operations.
CayenneForm Examples
The example below provides a
Department data object creation and editing page. To edit an existing department object, the object is passed to the page as a request parameter. Otherwise a new department object will be created when {@link #saveChanges()} is called.
public class OrganisationEdit extends Page { private CayenneForm form = new CayenneForm("form", Organisation.class); public OrganisationEdit() { form.add(new TextField("name", "Organisation Name:", 35); QuerySelect type = new QuerySelect("type", "Type:"); type.setQueryValueLabel("organisation-types", "VALUE", "LABEL"); form.add(type); form.add(new TextArea("description", "Description:", 35, 2); form.add(new Submit("ok", " OK ", this, "onOkClicked"); form.add(new Submit("cancel", this, "onCancelClicked"); form.setButtonAlign("right"); addControl(form); } public void onGet() { Organisation organisation = (Organisation) getContext().getRequestAttribute("organisation"); if (organisation != null) { form.setDataObject(organisation); } } public boolean onOkClicked() { if (form.isValid()) { if (form.saveChanges()) { Organisation organisation = (Organisation) form.getDataObject(false); String url = getContext().getPagePath(OrganisationViewer.class); setRedirect(url + "?id=" + organisation.getId()); return false; } } return true; } public boolean onCancelClicked() { Organisation organisation = (Organisation) form.getDataObject(false); String url = getContext().getPagePath(OrganisationViewer.class); setRedirect(url + "?id=" + organisation.getId()); return false; } }
Note the
getDataObject(false) method is used to obtain the DataObject from the Form without applying the field values to the data object. This is very important when dealing with already persistent objects and you dont want to apply any form changes.
Alternatively you can save a submitted DataObject using a Service or DAO pattern. For example:
public boolean onOkClicked() { if (form.isValid()) { Organisation organisation = (Organisation) form.getDataObject(); getOrganisationService().save(organisation); String url = getContext().getPagePath(OrganisationViewer.class); setRedirect(url + "?id=" + organisation.getId()); return false; } return true; }
Please Note if you are using this pattern with objects already saved, take care to ensure that the form submission is valid before calling {@link #getDataObject()} as this method changes the DataObject's propertiesusing the submitted form field values.
If you don't commit the changes at this point they will still be present in the session {@link DataContext} and will be applied in the next
commitChanges() call, which may happen in a subsequent request. In these exceptional situations the object should be removed from the cache DataContext using
invalidateObjects() method or by reloading the object from the database.
Alternatively use the {@link DataContextFilter} which willautomatically rollback any uncommitted changes at the end of each request.