我将 JPA 与 Hibernate 实现一起使用。我的@entity 事务如下:
@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();
运行此代码后id
,事务 t 内部是数据库自动生成的,但时间戳为null
.
我怎样才能以一种timestamp
一旦被调用也返回到对象的方式来制作thigs persist()
?
谢谢你