SpringDataJpa更新、删除操作
更新操作
具体代码如下:
@Test
public void testUpdate(){
//通过实体管理工厂获取实体管理器
EntityManager em = JpaUtils.getEntityManager();
//获取事务对象,开启事务
EntityTransaction tx = em.getTransaction();
tx.begin();
//完成增删改查操作
/**
* getReference:根据id查询数据
* class:查询数据的结果需要包装的实体类型的字节码
* id:查询的主键的取值
*/
Customer customer = em.getReference(Customer.class,1);
customer.setCustIndustry("学习");
em.merge(customer);
//提交事务
tx.commit();
//释放资源
em.close();
}
删除操作
具体代码如下:
@Test
public void testRemove(){
//通过实体管理工厂获取实体管理器
EntityManager em = JpaUtils.getEntityManager();
//获取事务对象,开启事务
EntityTransaction tx = em.getTransaction();
tx.begin();
//完成增删改查操作
/**
* getReference:根据id查询数据
* class:查询数据的结果需要包装的实体类型的字节码
* id:查询的主键的取值
*/
Customer customer = em.getReference(Customer.class,1);
em.remove(customer);
//提交事务
tx.commit();
//释放资源
em.close();
}