使用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是我实体中唯一的空字段。
任何建议都将不胜感激。
谢谢!
答案 0 :(得分:0)
查看hibernate文档的 "Generated properties" 部分。根据您的使用情况使用ALWAYS或INSERT进行注释
@Generated(GenerationTime.INSERT)