我正在使用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