我在使用 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 放入 Not-null 列。CreateTimestamp 是我的实体中唯一为空的字段。
任何建议将不胜感激。
谢谢!