04 November 2010

Get the value last saved from DB

Last upate: 9.12.2010
Call getPostedAttribute() anywhere in your Entity Object implementation class to retrieve the posted value of an Entity attribute.


API Ref:
protected java.lang.Object getPostedAttribute(int index)
Gets the value originally read for this attribute from the database.
If the attribute value at the specified index has been changed (whether to the same value or a new value), return the original value as read from the database on first query. The method invokes getAttributeInternal to return the original value.
This method should not be overridden.
Parameters:
index - index of the attribute.
Returns:
attribute value as currently posted in the database.


Reposting a nice example from Nick's blog

  // in your Entity Object Implementation class

    @Override
    protected void doDML(int operation, TransactionEvent e) {

        final String EMPLOYEE_ID = "EmployeeId";
      
        // get posted value of EmployeeId attribute
        Object postedEmployeeId = getPostedAttribute(this.getAttributeIndexOf(EMPLOYEE_ID));
      
        // get value of EmployeeId before re-posting
        Object employeeId = this.getAttribute(EMPLOYEE_ID);
      
        // compare and take some action based on the results of comparison
        if (employeeId != null && employeeId.equals(postedEmployeeId)) {
          // do something here
        }
      
        // finally re-post by calling super.doDML()
        super.doDML(operation, e);
    }



More on Nick's post: http://adfcodebits.blogspot.com/2010/07/bit-22-using-getpostedattribute-to.html


Also check Jobinesh's nice post regarding "A public API to get the value originally read for an Entity Attribute from the database".
Reposting  some part:
There is an 'undocumented and smart' API to get get the value originally read for an Entity Attribute from the database. Thanks toSteve for sharing this. You may need to callEntityImpl::getAttribute(attrIndex,EntityImpl.ORIGINAL_VERSION) to get the original value of the attribute....


No comments:

Post a Comment

You might also like:

Related Posts Plugin for WordPress, Blogger...
There was an error in this gadget