06-根据id查询客户
作者:互联网
@Test
public void testFind(){
//1、通过工具类获取entityManager
EntityManager entityManager = JpaUtils.getEntityManager();
//2、开启事务
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
/*
3、增删改查---根据id查询客户
find:根据id查询客户
class:查询数据的结果需要包装的实体类类型的字节码
id:查询的主键的取值
*/
Customer customer = entityManager.find(Customer.class, 1l);
System.out.println(customer);
//4、提交事务
tx.commit();
//5、释放资源
entityManager.close();
}
@Test
public void testReference(){
//1、通过工具类获取entityManager
EntityManager entityManager = JpaUtils.getEntityManager();
//2、开启事务
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
/*
3、增删改查---根据id查询客户
getReference:根据id查询客户
class:查询数据的结果需要包装的实体类类型的字节码
id:查询的主键的取值
*/
Customer customer = entityManager.getReference(Customer.class, 1l);
System.out.println(customer);
//4、提交事务
tx.commit();
//5、释放资源
entityManager.close();
}
标签:Customer,entityManager,06,tx,class,查询,id 来源: https://www.cnblogs.com/morehair/p/15486805.html