@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 + "]";
}
}
I'm expecting the value '15-JUL-1980' to be inserted for each row IF I DONOT set its value.
But its not working as expected. Am I doing anything wrong here?
For some reasons, I want to set the default value at the application and Not at the database.
UPDATE:
Initially i tried without 'insertable = false' only.
As a newbie, I was trying different options and had kept that option.
Here is the test case which I'm running:
@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);
}
and the output it has produced:
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]
Table:
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")
}