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();
            }
        }
  • 相关阅读:
    JAVA基础知识之多线程——三种实现多线程的方法及区别
    JAVA基础知识之Collections工具类
    JAVA基础知识之Map集合
    JAVA基础知识之Queue集合
    JAVA基础知识之List集合
    JAVA基础知识之Set集合
    Java基础知识之集合(容器)简介
    JAVA中STL使用
    博客园皮肤
    RMQ
  • 原文地址:https://www.cnblogs.com/roadlandscape/p/12371614.html
Copyright © 2011-2022 走看看