我无法更新记录,因此:
package legacy.database;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
public class Queries {
private static final Logger log = Logger.getLogger(Queries.class.getName());
private final EntityManagerFactory emf = Persistence.createEntityManagerFactory("LegacyDatabasePU");
private final EntityManager em = emf.createEntityManager();
public Queries() {
}
private List<Clients> findAll() {
Query q = em.createQuery("select c from Clients c");
List<Clients> clients = q.getResultList();
return clients;
}
public Clients findById(int id) {
Clients client = em.find(Clients.class, id);
return client;
}
public void update(Clients c2) {
Clients c1 = em.find(Clients.class, c2.getId());
java.util.Date date = new java.util.Date();
Timestamp t = new Timestamp(date.getTime());
c2.setDateUpdated(t.toString());
em.getTransaction().begin();
c2.setDateUpdated(t.toString());
c1 = c2;
log.info(c1.getNotes());
log.info(c2.getNotes());
em.getTransaction().commit();
}
}
分配c1 = c2有问题吗?我是否必须手动浏览c1并更新每条记录?
答案 0 :(得分:2)
在事务中执行find(),然后在事务中调用您的变异方法。然后,Commit将自动更新对象。只需简单的开始txn运动即可。只有你知道各种更新是什么,所以不试图清理它
try
{
em.getTransaction().begin();
Clients c1 = em.find(Clients.class, c2.getId());
Timestamp t = new Timestamp(new java.util.Date().getTime());
c2.setDateUpdated(t.toString());
c1 = c2;
log.info(c1.getNotes()); log.info(c2.getNotes());
em.getTransaction().commit();
}
finally
{
if (em.getTransaction().isActive())
{
em.getTransaction().rollback();
}
}
答案 1 :(得分:1)
如果实体未在EntityManager
中注册,可能与c2
一样,那么除非您使用em.persist
,em.merge
等,否则不会更新实体。
而c1 = c2
只是将c1
引用指向c2
引用。
从下面的评论中,您要做的是将c2
的属性复制到c1
,这可以完成
在这两种情况下,你必须记住:
OneToMany
,您应该复制子实体。