使用JPA完成简单的增删改查操作

/**
     * 新增客户
     */
    @Test
    public void testAdd() {
        EntityManagerFactory factory = null;
        EntityManager em = null;
        EntityTransaction tx = null;

        try {
            factory = Persistence.createEntityManagerFactory("myJPA");
            em = factory.createEntityManager();
            tx = em.getTransaction();
            tx.begin();
            Customer c = new Customer();
            c.setCustName("老王");
            em.persist(c);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
        } finally {
            em.close();
            factory.close();
        }
    }
/**
     * 根据id查询客户 立即加载
     */
    @Test
    public void testFindById() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJPA");
        EntityManager em = factory.createEntityManager();
        EntityTransaction tx = em.getTransaction();

        tx.begin();

        Customer customer = em.find(Customer.class, 1L);
        System.out.println(customer);

        tx.commit();
        em.close();
        factory.close();
    }
/**
     * 根据id查询客户 懒加载
     */
    @Test
    public void testGetReference() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJPA");
        EntityManager em = factory.createEntityManager();
        EntityTransaction tx = em.getTransaction();

        tx.begin();

        /**
         * 延迟加载(懒加载)
         *  得到的是一个动态代理对象
         *  什么时候用,什么时候才会查询
         * getReference方法
         *  1.获取的对象是一个动态代理对象
         *  2.调用getReference方法不会立即发送sql语句查询数据库
         *    当调用查询结果对象的时候,才会发送查询的sql语句:什么时候用,什么时候发送sql语句查询数据库
         */
        Customer customer = em.getReference(Customer.class, 1L);
        System.out.println(customer);

        tx.commit();
        em.close();
        factory.close();
    }
/**
     * 更新客户
     */
    @Test
    public void testMerge() {
        EntityManagerFactory factory = null;
        EntityManager em = null;
        EntityTransaction tx = null;

        try {
            factory = Persistence.createEntityManagerFactory("myJPA");
            em = factory.createEntityManager();
            tx = em.getTransaction();
            tx.begin();
            Customer c = em.find(Customer.class, 3L);
            c.setCustName("小昭");
            em.merge(c);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
        } finally {
            em.close();
            factory.close();
        }
    }
/**
     * 删除客户
     */
    @Test
    public void testRemove() {
        EntityManagerFactory factory = null;
        EntityManager em = null;
        EntityTransaction tx = null;

        try {
            factory = Persistence.createEntityManagerFactory("myJPA");
            em = factory.createEntityManager();
            tx = em.getTransaction();
            tx.begin();
            Customer c = em.find(Customer.class, 2L);
            em.remove(c);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
        } finally {
            em.close();
            factory.close();
        }
    }

 

posted @ 2020-02-27 13:02  糖不甜,盐不咸  阅读(395)  评论(0编辑  收藏  举报