具有数据库生成的时间戳的Hibernate复合密钥

具有数据库生成的时间戳的Hibernate复合密钥,hibernate,Hibernate,使用Hibernate 3.3和注释保存具有复合主键的对象时遇到问题 我的密钥由用户的唯一ID和数据库生成时间戳组成 如何使用注释映射此键?我使用了一些逆向工程工具,但当我试图保存实体时,我遇到了问题 例如: @Entity @Table(name = "ACTIVITY_LOG", schema = "WEB") public class ActivityLog implements java.io.Serializable { // Fields private Acti

使用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
这告诉我我不能把null放在NOTNULL列中。CreateTimestamp是实体中唯一为空的字段

如有任何建议,将不胜感激

谢谢

看看hibernate文档的一节。根据您的用例,使用ALWAYS或INSERT进行注释

@Generated(GenerationTime.INSERT)