在实体类的那个字段上添加
@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();
执行效果