2010-06-21 56 views
1

我有在所有域對象中可用的字段。這些字段是:休眠ORM值對象中的常見字段

Created_By 
Created_Date 
Modified_By 
Modified_Date 

我可以聲明/讓CoreBusinessObject包含這四個字段並將其擴展到另一個對象嗎?這是好的設計嗎?如果是這樣,我怎麼做ORM映射?

+0

對不起。我必須使用ORM映射。我該怎麼做? – Jothi 2010-06-21 10:47:14

+0

ORM =對象關係映射。所以「ORM並不意味着什麼,也許你的意思是」xml mapping「? – Bozho 2010-06-21 13:22:48

回答

1

我推薦以下設計....

首先,你需要一個LifeCycleInfo組件....

相關創建的持久化模型實例時,最後修改爲LifeCycleInfo店信息,刪除並存檔。

<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> 

然後,你需要一個標記接口說的,可審計

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. */ 

} 

然後,你的持久化模型實現的可審計

Employee implements Auditable 

然後,你需要的Hibernate攔截...

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 */ 

}

然後,註冊您的可審計攔截.....

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

你就大功告成了!

  • SE

編輯:

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); 
} 
.................... 
0

是的,這是可能的,它也是很好的設計。

如果您使用Hibernate Annotations @MappedSuperclass是您正在尋找的。簡單地用你記下的4個字段創建一個抽象類,然後任何時候@Entity都有這4個字段,它們從父抽象類繼承。

有關更多信息,可以在http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e1168處看到hibernate註釋文檔。

+0

對不起,我必須使用ORM映射,我該怎麼做呢? – Jothi 2010-06-21 10:46:47