tags:

views:

47

answers:

2

I have feilds which is available in all domain obejcts.Those feilds are, created_By,Created_date, Modified_By, Modified Date. Can i declare/have like CorebusinessObject which contains these four feilds, and extending this into other object. Is it good design? If so, How could i do ORM mapping.

A: 

Yes, this is possible and its also good design.

If you are using Hibernate Annotations @MappedSuperclass is what you're looking for. Simply make an abstract class with the 4 fields you noted, then for any time an @Entity has those 4 fields, inherit from the parent abstract class.

You can see the hibernate annotations documentation at http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e1168 for more information.

tschaible
Sorry. I have to use ORM mapping. How do i do it?
Jothi
A: 

I'd recommend following design....

First, you need a LifeCycleInfo component....

The LifeCycleInfo stores information related to when the Persistent Model instance was created, last modified, deleted and archived.

<hibernate-mapping package="com.comp.model">
<class name="Employee" table="employee" lazy="false">
    <id name="id">
         <generator class="assigned"/>
    </id>

    <property name="email" type="string" column="email"/>

    <!-- Model lifecycle info attributes -->
    <component name="LifeCycleInfo" class="com.comp.component.LifeCycleInfo">
        <property name="createdDate" type="timestamp"  column="created_date"/>
        <property name="createdBy" type="string" column="created_by"/>
        <property name="lastModifiedDate" type="timestamp" column="last_modified_date"/>
        <property name="lastModifiedBy" type="string" column="last_modified_by"/>
        <property name="deletedBy" type="string" column="del_by"/>
        <property name="deletedDate" type="timestamp" column="del_date"/>
        <property name="archivedBy" type="string" column="arc_by"/>
        <property name="archivedDate" type="timestamp" column="arc_date"/>
    </component>
</class>

Then, you need a marker interface say, Auditable

public  interface Auditable  {
    Date getCreatedDate ();
void setCreatedDate (Date crt_date);

String getCreatedBy ();
void setCreatedBy (String user_id);

    /* rest of the methods for getting/setting lastModifiedDate/By etc. */

}

Then, your Persistent Model implements Auditable

Employee implements Auditable

Then, you need Hibernate Interceptor ...

public class AuditableInterceptor extends EmptyInterceptor {

 public boolean onSave(Object entity, Serializable id, Object[] state,
                      String[] propertyNames, Type[] types) 
{
    if ( entity instanceof Auditable ) 
    {
        Date now = new Date();

        String userId = factory.getUserName(); /* get user Id from somewhere*/

        ((Auditable)entity).setCreatedDate(now);
        ((Auditable)entity).setCreatedBy(userId);

        return true;
     }
    return false;
}

/* rest of the implementation is trivial */

}

Then, register your Auditable Interceptor.....

Configuration cfg = new Configuration();
cfg.setInterceptor(new AuditableInterceptor());
.......
/* build SessionFactory */

You're Done!

  • SE

EDIT:

Employee.java

 ...............
  public Date getCreatedDate()
{
    return this.getLifeCycleInfo().getCreatedDate();
}

    public void setCreatedDate(Date createdDate)
{
    if (this.getLifeCycleInfo() == null)
        this.setLifeCycleInfo(new LifeCycleInfo());
    this.getLifeCycleInfo().setCreatedDate(createdDate);
} 
....................
becomputer06