当前位置: 代码迷 >> 综合 >> JPA执行持久化代码
  详细解决方案

JPA执行持久化代码

热度:93   发布时间:2023-10-26 08:56:41.0
                //创建 EntityManagerFactory 对象EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("jpa-1");//创建 EntityManager 对象EntityManager entityManager = entityManagerFactory.createEntityManager();//开启事务EntityTransaction transaction = entityManager.getTransaction();transaction.begin();Customer customer = new Customer();customer.setName("xxx");customer.setEmail("xxx@163.com");customer.setAge(23);customer.setBirth(new Date());customer.setCreateDate(DateTime.now());customer.setLastUpdateDate(LocalDate.now());//持久化操作entityManager.persist(customer);//提交事务transaction.commit();//关闭  EntityManagerentityManager.close();//关闭  EntityManagerFactoryentityManagerFactory.close();

  相关解决方案