无法重现.我在Derby上测试了以下实体:
@Entity
public class EntityWithGeneratedField {
@Id
@GeneratedValue
private Long id;
@Column(name = "LAST_UPDATED_DTTM", insertable = false, updatable = false,
columnDefinition = "Date default CURRENT_DATE")
@org.hibernate.annotations.Generated(value = GenerationTime.INSERT)
@Temporal(javax.persistence.TemporalType.DATE)
private Date lastUpdDTTM;
// getters, setters
}
以下测试方法通过:
@Test
public void testDateGenerationWhenInsertingEntityWithNullDate() {
EntityWithGeneratedField e = new EntityWithGeneratedField();
session.persist(e);
session.flush();
assertNotNull(e.getId());
assertNotNull(e.getLastUpdDTTM());
}
下面,生成的SQL:
Hibernate: insert into EntityWithGeneratedField (id) values (?)
Hibernate: select entitywith_.LAST_UPDATED_DTTM as LAST2_83_ from EntityWithGeneratedField entitywith_ where entitywith_.id=?
一切都按预期工作.
我的建议:检查表的DDL,检查事情是否与原始SQL INSERT一起使用.我怀疑在数据库级别存在问题,而不是在Hibernate级别.