一、EntityManager各种方法
1、persist
persist把实体类进行持久化,相当于增加操作
User user = new User();
user.setSex(false);
user.setName("小白");
user.setBirth(new Date());
user.setAge(18);
entityManager.persist(user);
2、find
find 会根据id把数据库的实体类查询出来
@Test
public void findTest(){
User user = entityManager.find(User.class, 1);
System.out.println(user);
}
3、Reference
reference 会根据id把数据库的实体类查询出来 但是用实体类的时候他才会查询出来
@Test
public void refrenceTest(){
User user = entityManager.getReference(User.class, 1);
System.out.println("---------------------");
System.out.println(user);
}
4、remove
remove 会把根据查出来的数据删除对用字段
@Test
public void removeTest(){
User user = entityManager.find(User.class, 1);
entityManager.remove(user);
System.out.println(user);
}
5、merge
是根据转入的实体类状态对数据库进行增加或者进行修改
Customer customer=new Customer();
customer.setLastName("fu");
customer.setEmail("qq@.com");
customer.setBirth(new Date());
customer.setCreatedTime(new Date());
customer.setAge(15);
Customer customer1 = entityManager.merge(customer);
System.out.println("customer01: "+customer.getId());
System.out.println("customer02: "+customer1.getId());
6、flush
会把查出来的数据跟数据库同步
Customer customer = entityManager.find(Customer.class, 1);
System.out.println(customer);
customer.setLastName("BB");
entityManager.flush();