我正在使用JPA与Hibernate实现。 我有@Entity交易如下:JPA字段时间戳不更新
@Entity
public class Transaction {
private int id;
private Date timestamp;
...
@Basic
@Column(name = "timestamp", insertable = false, updatable = true)
@Temporal(TemporalType.TIMESTAMP)
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
...
@Column(name = "id")
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "transaction_id_seq")
@SequenceGenerator(name = "transaction_id_seq", sequenceName = "transaction_id_seq", allocationSize = 1)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
当我创建一个新的事务,我不设置id
和timestamp
领域,我使用persist()
PersistenceProvider pp = new HibernatePersistence();
EntityManagerFactory emf = pp.createEntityManagerFactory("pu", new HashMap());
EntityManager em = emf.createEntityManager();
Transaction t = new Transaction();
em.getTransaction().begin();
em.persist(t);
em.getTransaction().commit();
其保存在数据库中
运行此代码后,Transaction t中的id
是由DB自动生成的代码,但时间戳为null
。
我怎样才能让timestamp
返回到对象一次的方式persist()
它被称为?
谢谢
我使用Postgresql,但我没有指定时间戳是由数据库使用函数生成correclty now() – hurtledown