zoukankan      html  css  js  c++  java
  • 使用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();
            }
        }
  • 相关阅读:
    亚马逊云IoT平台接入开发记录
    pip下载速度慢更换清华源试试
    gitlab回归上一次提交
    uos桌面壁纸存放路径
    python中json中的dump和dumps
    Python中的类中__dict__方法
    C++ | 数组反转的三种方法
    《C++Primer Plus》 | 复合类型
    pwn 中的函数 | 持续更新
    七月安恒DASCTF | 复现
  • 原文地址:https://www.cnblogs.com/roadlandscape/p/12371614.html
Copyright © 2011-2022 走看看