Hibernate updating records and implementing listeners : getting only required attribute values for event.getOldState()
I am using Hibernate 3 as my persistence framework. Below is the sample hbm file I am using.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.test.User" 开发者_如何学JAVAtable="user">
<meta attribute="implements">com.test.dao.interfaces.IEntity</meta>
<id name="key" type="long" column="user_key">
<generator class="increment" />
</id>
<property name="userName" column="user_name" not-null="true" type="string" />
<property name="password" column="password" not-null="true" type="string" />
<property name="firstName" column="first_name" not-null="true" type="string" />
<property name="lastName" column="last_name" not-null="true" type="string" />
<property name="createdDate" column="created_date" not-null="true" type="timestamp" insert="false" update="false" />
<property name="createdBy" column="created_by" not-null="true" type="string" update="false" />
</class>
</hibernate-mapping>
I am added a post-update listener. What it will do is if there any updations perfomed on User then it will be invoked and cahnges will be inserted to audit table.
Below is the sample implementation for postupdate event.
public void onPostUpdate(PostUpdateEvent event)
{
LogHelper.info(logger, "Begin - onPostUpdate "
+ event.getEntity().getClass().getSimpleName());
if (!this.checkForAudit(event.getEntity().getClass().getSimpleName()))
{
// check do we need to audit it.
}
// Get Attribute Names
String[] attrNames = event.getPersister().getEntityMetamodel()
.getPropertyNames();
Object[] oldobjectValue = c
Object[] newObjectValue = event.getState();
this.auditDetailsEvent(attrNames, oldobjectValue, newObjectValue);
LogHelper.info(logger, "End - onPostUpdate");
// return false;
}
Here is my requirement. event.getPersister().getEntityMetamodel() .getPropertyNames(); or event.getOldState(); or event.getState();
must return attribute names or value which i can update or insert.
Is there any way to control the return values of above one's.
Pleas help me on this regard.
Thanks,
Narendra
Here is my requirement. event.getPersister().getEntityMetamodel() .getPropertyNames(); or event.getOldState(); or event.getState(); must return attribute names or value which i can update or insert.
Not sure I understand... Do you want these specific methods to return a collection of attribute names and/or values, so that you use only these in a separate SQL insert/update clause into the Audit table? Are you inserting on that table on a per-attribute basis?
Also, have you read about Hibernate Envers? Envers is the Hibernate "extension" for auditing purposes: http://docs.jboss.org/envers/docs/index.html
The Problem Can be solved versy simply by using PostInsertEvent.getPersister().getPropertyInsertability();
It will return a boolean array . So by checking this array we can identify is it necessaty to insert them in audit table or not.
精彩评论