【发布时间】:2010-09-12 17:41:06
【问题描述】:
@Entity
@Table(name = "J_CNTRY")
public class CountryEntity {
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "myTableGenerator")
@TableGenerator(name = "myTableGenerator", allocationSize = 5, pkColumnName = "pkName", valueColumnName = "pkValue", table = "j_cntry_pk_table")
private Long id;
private String country;
@Generated(GenerationTime.INSERT)
@Column(name = "CREATION_TIME", columnDefinition = "DATE default '15-JUL-1980'", insertable = false)
private Date creationTime;
public CountryEntity() {
}
public Long getId() {
return id;
}
public void setId(final Long id) {
this.id = id;
}
public String getCountry() {
return country;
}
public void setCountry(final String country) {
this.country = country;
}
public Date getCreationTime() {
return creationTime;
}
@Override
public String toString() {
return "Country [id=" + id + ", country=" + country + ", creationTime="
+ creationTime + "]";
}
}
如果我不设置其值,我希望为每一行插入值“15-JUL-1980”。
但它没有按预期工作。我在这里做错了吗?
由于某些原因,我想在应用程序中设置默认值,而不是在数据库中。
更新:
最初我只尝试不使用 'insertable = false'。
作为一个新手,我尝试了不同的选择并保留了那个选择。
这是我正在运行的测试用例:
@Test
public void testCreateCountry4() {
final CountryEntity a1 = new CountryEntity();
a1.setCountry("Xxxx");
final Session currentSession = sessionFactory.getCurrentSession();
final Long savedId = (Long) currentSession.save(a1);
System.out.println("Saved with ID = " + savedId);
currentSession.flush();
System.out.println("Saved object = " + a1);
assertNotNull(savedId);
}
以及它产生的输出:
Saved with ID = 85
Hibernate:
/* insert entities.CountryEntity
*/ insert
into
J_CNTRY
(country, id)
values
(?, ?)
Hibernate:
/* get generated state entities.CountryEntity */ select
countryent_.CREATION_TIME as CREATION3_2_
from
J_CNTRY countryent_
where
countryent_.id=?
Saved object = Country [id=85, country=Xxxx, creationTime=null]
表:
CREATE TABLE "FOO"."J_CNTRY"
( "ID" VARCHAR2(255 BYTE) NOT NULL,
"COUNTRY" VARCHAR2(255 BYTE),
"CREATION_TIME" DATE,
CONSTRAINT "J_CNTRY_PK" PRIMARY KEY ("ID")
}
【问题讨论】:
-
其实问题出在列定义
"CREATION_TIME" DATE DEFAULT NULL,对吧?这个表是由 Hibernate 生成的吗?从头开始? -
@Pascal:我是 Hibernate 的新手。我希望 Hibernate 在我尝试插入时设置默认值。我展示的表格是我为测试/学习目的而创建的虚拟表格。原始表是旧表,它没有设置“默认”属性。很抱歉造成混乱。
标签: hibernate annotations default-value oracle9i hibernate-annotations