1. 程式人生 > >002-原始jpa以及基本加載過程

002-原始jpa以及基本加載過程

參數 clas ide 問題 tee update comm 關閉 getname

一、概述

1.1、創建

    public void createMethod() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("");
        EntityManager manager = factory.createEntityManager();
        manager.getTransaction().begin();// 開始事務
        manager.persist(new Object());
        manager.getTransaction().commit();
        manager.close();
        factory.close();
    }

1.2、查詢

    // 立即加載
    public void getMethod() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("");
        EntityManager manager = factory.createEntityManager();
        Object object = manager.find(Object.class, 1);
        System.out.println(object);// 如果Object是null,沒有異常打印null
manager.close(); factory.close(); } // 延遲加載 public void getMethod2() { EntityManagerFactory factory = Persistence.createEntityManagerFactory(""); EntityManager manager = factory.createEntityManager(); // 延遲加載,不立即返回,返回一個代理對象,只有讀取對象具體屬性時,才返回具體對象
Object object = manager.getReference(Object.class, 1); // 如果數據庫沒有object,在讀取屬性是出現異常 // object.getName(); manager.close(); // 關閉後再讀取有問題 // object.getName(); factory.close(); }

1.3、更新

    public void updateMethod() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("");
        EntityManager manager = factory.createEntityManager();
        manager.getTransaction().begin();// 開始事務
        Object object = manager.find(Object.class, 1);
        object,setName("測試");
        manager.getTransaction().commit();
        manager.close();
        factory.close();
        // 四種 實體狀態:new新建狀態; managed托管狀態,能夠被更新【托管狀態,被事務管理】,放到jdbc批處理中,commit之後開始提交;遊離狀態【托管】;刪除狀態
    }
    public void updateMethod2() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("");
        EntityManager manager = factory.createEntityManager();
        manager.getTransaction().begin();// 開始事務
        Object object = manager.find(Object.class, 1);
        manager.clear();//把實體管理器中的所有實體變成遊離狀態,此時提交;不會被更新,需要做以下處理
        object,setName("測試");
        manager.merge(object);//把實體放入實體管理器中,再次提交可以被更新
        manager.getTransaction().commit();
        manager.close();
        factory.close();
        // 四種 實體狀態:new新建狀態; managed托管狀態,能夠被更新【托管狀態,被事務管理】,放到jdbc批處理中,commit之後開始提交;遊離狀態【托管】;刪除狀態
    }

1.4、刪除

    public void deleteMethod() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("");
        EntityManager manager = factory.createEntityManager();
        manager.getTransaction().begin();// 開始事務
        Object object = manager.find(Object.class, 1);
        manager.remove(object);
        manager.getTransaction().commit();
        manager.close();
        factory.close();
    }

二、基本加載過程

EntityManagerFactory factory = Persistence.createEntityManagerFactory("");

查看:Persistence,javax.persistence.Persistence,其中:createEntityManagerFactory代碼如下

    public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {
        EntityManagerFactory emf = null;
        List<PersistenceProvider> providers = getProviders();//1
        for ( PersistenceProvider provider : providers ) {
            emf = provider.createEntityManagerFactory( persistenceUnitName, properties );//2
            if ( emf != null ) {
                break;
            }
        }
        if ( emf == null ) {
            throw new PersistenceException( "No Persistence provider for EntityManager named " + persistenceUnitName );
        }
        return emf;
    }

對於1句,

    private static List<PersistenceProvider> getProviders() {
        return PersistenceProviderResolverHolder
                .getPersistenceProviderResolver()
                .getPersistenceProviders();
    }

查看代碼,查看PersistenceProviderResolverHolder的私有靜態類CachingPersistenceProviderResolver,其中讀取

Enumeration<URL> resources = cl.getResources( "META-INF/services/" + PersistenceProvider.class.getName() );

對於第二句

provider.createEntityManagerFactory(

選擇任意一個持久化接口創建

三、sql語句的使用【增刪改查】

    public void sqlQueryMethod() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("");
        EntityManager manager = factory.createEntityManager();
        // 面向對象查詢語句
        // 位置參數
        Query query = manager.createQuery("selet o from Person o where o.id=?1");
        query.setParameter(1, 222);
        // 類型參數
        // Query query2 = manager.createQuery("selet o from Person o where o.id=:id");
        // query2.setParameter("id", 222);
        
        List resultList = query.getResultList();
        
        //如果只有一個值
        //query.getSingleResult();
        manager.close();
        factory.close();
    }
    public void sqlDeleteMethod() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("");
        EntityManager manager = factory.createEntityManager();
        manager.getTransaction().begin();// 開始事務
        // 面向對象查詢語句
        // 位置參數
        Query query = manager.createQuery("delete from Person o where o.id=?1");
        query.setParameter(1, 222);
        query.executeUpdate();
        manager.getTransaction().commit();
        manager.close();
        factory.close();
    }
    public void sqlUpdateMethod() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("");
        EntityManager manager = factory.createEntityManager();
        manager.getTransaction().begin();// 開始事務
        // 面向對象查詢語句
        // 位置參數
        Query query = manager.createQuery("update Person 0 set o.name=:name where  o.id=:id");
        query.setParameter("name", "zahangsan");
        query.setParameter("id", 11);
        query.executeUpdate();
        manager.getTransaction().commit();
        manager.close();
        factory.close();
    }

002-原始jpa以及基本加載過程