Hibernate中設定實體的屬性為當前系統時間
阿新 • • 發佈:2019-01-31
在實體類的那個欄位上新增
@Column(nullable=true,columnDefinition="timestamp default current_timestamp")
小示例:
@Entity
@Table(name="person_inf")
public class Person {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
@Column(nullable=true,columnDefinition="timestamp default current_timestamp" )
private Date birth;
//省略set和get
}
測試程式碼
Session session = DBTool.getSession();
session.beginTransaction();
for(int i = 0; i < 10; i++) {
Person person = new Person();
person.setName("第" + i + "個人");
System.out.println(System.currentTimeMillis ());
session.save(person);
}
session.getTransaction().commit();
執行效果