保存
public void testSave(){
EntityManager entityManager = JpaUtils.getEntityManager();
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
Customer customer = new Customer();
customer.setCustName("LEEWLE");
customer.setCustIndustry("体育");
entityManager.persist(customer);
tx.commit();
entityManager.close();
}
删除
@Test
public void testRemove(){
EntityManager entityManager = JpaUtils.getEntityManager();
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
Customer customer = entityManager.find(Customer.class, 1l);
entityManager.remove(customer);
tx.commit();
entityManager.close();
}
更新
@Test
public void testUpdate(){
EntityManager entityManager = JpaUtils.getEntityManager();
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
Customer customer = entityManager.find(Customer.class, 2l);
customer.setCustIndustry("美术");
entityManager.merge(customer);
tx.commit();
entityManager.close();
}
查询
@Test
public void testFind(){
EntityManager entityManager = JpaUtils.getEntityManager();
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
Customer customer = entityManager.find(Customer.class, 1l);
System.out.println(customer);
tx.commit();
entityManager.close();
}
@Test
public void testReference(){
EntityManager entityManager = JpaUtils.getEntityManager();
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
Customer customer = entityManager.getReference(Customer.class, 1l);
System.out.println(customer);
tx.commit();
entityManager.close();
}