2012-10-26 95 views
1

我遇到麻煩保存一個複合主鍵使用Hibernate 3.3和註釋的對象。休眠與數據庫生成的時間戳複合關鍵

我的密鑰由用戶的唯一ID和數據庫生成時間戳組成。

如何使用註釋映射此密鑰?我使用了一些反向工程工具,但是當我嘗試保存實體時遇到了問題。

例子:

@Entity 
@Table(name = "ACTIVITY_LOG", schema = "WEB") 
public class ActivityLog implements java.io.Serializable { 

    // Fields 

    private ActivityLogId id; 
    private SearchAuditRecord searchAuditRecord; 

    // Constructors 

     ..... 

    // Property accessors 
    @EmbeddedId 
    @AttributeOverrides({ 
     @AttributeOverride(name = "identifier", column = @Column(name = "IDENTIFIER", nullable = false, length = 11)), 
     @AttributeOverride(name = "createTimestamp", column = @Column(name = "CREATE_TIMESTAMP", insertable=false, updatable=false, nullable = false, length = 26)) }) 
    public ActivityLogId getId() { 
    return this.id; 
    } 

    public void setId(ActivityLogId id) { 
    this.id = id; 
    } 

    ..... 

} 


@Embeddable 
public class ActivityLogId implements java.io.Serializable { 

    // Fields 

    private String identifier; 
    private Date createTimestamp; 

    // Constructors 

    /** default constructor */ 
public ActivityLogId() { 
    } 

    /** full constructor */ 
    public ActivityLogId(String hrmisIdentifier, Date createTimestamp) { 
    this.identifier = identifier; 
    this.createTimestamp = createTimestamp; 
} 

// Property accessors 

    @Column(name = "IDENTIFIER", nullable = false, length = 11) 
    public String getIdentifier() { 
     return this.identifier; 
    } 

    public void setIdentifier(String identifier) { 
     this.identifier = identifier; 
    } 


    @Column(name = "CREATE_TIMESTAMP",insertable= false, updatable= false, nullable = false, length = 26) 
    public Date getCreateTimestamp() { 
     return this.createTimestamp; 
    } 

    public void setCreateTimestamp(Timestamp createTimestamp) { 
     this.createTimestamp = createTimestamp; 
    } 

} 

我想我需要告訴Hibernate正由數據庫中創建ActivityLogID.createTimestamp ...但我不知道怎麼樣。

因爲它站在現在,當我嘗試保存對象的論文,我得到:

org.hibernate.exception.ConstraintViolationException: 
could not insert: [ca.gc.rcmp.persistence.ActivityLog]........ 
Caused by: com.ibm.db2.jcc.am.fo: DB2 SQL Error: SQLCODE=-407, SQLSTATE=23502, SQLERRMC= , DRIVER=3.57.82 

這是告訴我,我不能把一個空到非空列。 CreateTimestamp是我的實體中唯一的爲空的字段。

任何意見將不勝感激。

謝謝!

回答