Hibernate 更新记录并实现侦听器:仅获取 event.getOldState() 所需的属性值
我使用 Hibernate 3 作为我的持久性框架。 下面是我正在使用的示例 hbm 文件。
<?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" table="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>
我添加了一个更新后监听器。它将做的是,如果对用户执行任何更新,那么它将被调用,并且更改将被插入到审计表中。
以下是更新后事件的示例实现。
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;
}
这是我的要求。 event.getPersister().getEntityMetamodel() .getPropertyNames();或 event.getOldState();或 event.getState();
必须返回我可以更新或插入的属性名称或值。
有没有办法控制上面的返回值。
请在这方面帮助我。
谢谢,
纳伦德拉
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" table="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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定我是否理解...您是否希望这些特定方法返回属性名称和/或值的集合,以便您仅在审计表中的单独 SQL 插入/更新子句中使用这些?您是否在每个属性的基础上插入该表?
另外,您读过 Hibernate Envers 吗? Envers 是用于审计目的的 Hibernate“扩展”: http://docs.jboss.org/ envers/docs/index.html
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
通过使用 PostInsertEvent.getPersister().getPropertyInsertability(); 可以非常简单地解决问题
它将返回一个布尔数组。因此,通过检查这个数组,我们可以确定是否有必要将它们插入审计表中。
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.