2015-04-03 70 views
1

我試圖以編程方式創建實體廠長沒有持久性文件,編程創建實體管理器,而不持久性文件,

EntityManagerFactory emf; 
    Map<String, String> properties = new HashMap<String, String>(); 
    properties.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); 
    properties.put("hibernate.connection.url", "jdbc:mysql://173.194.25***************"); 
    properties.put("hibernate.connection.username", "etech****"); 
    properties.put("hibernate.connection.password", "A*****"); 
    properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); 
    properties.put("hibernate.show-sql", "true"); 
    properties.put("provider", "org.hibernate.ejb.HibernatePersistence"); 
    emf = Persistence.createEntityManagerFactory(idClient, properties); 

在線

emf = Persistence.createEntityManagerFactory(idClient, properties); 

我收到錯誤:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com****RepositoryFieldsFieldWorkerRepositoryImpl': Invocation of init method failed; nested exception is javax.persistence.PersistenceException: No Persistence provider for EntityManager named idClient

我該如何解決這個問題?

任何幫助將不勝感激

+0

視覺變化?這是什麼意思 ? – tamtoum1987 2015-04-03 15:24:11

+0

其實這個評論是打算在框中,我告訴你我編輯時提出了什麼改變... – rfornal 2015-04-03 15:37:11

回答

0

一個persistence.xml文件是必須在部署時按照JPA規範中,以創建持久性單元。

Create JPA EntityManager without persistence.xml configuration file

+0

我創建了一個persistence.xml文件,但我有同樣的問題 <?xml version =「1.0」encoding = 「UTF-8」?> javax.persistence.spi.PeristenceProvider tamtoum1987 2015-04-03 15:47:16

+0

您是否重新部署了您的應用?日誌應包含確認PU已正確加載。如果在創建Emf時指定提供程序,則不需要在persistence.xml文件中指定提供程序。或者更好,如果你不需要在運行時改變它們,把所有的屬性放在persistence.xml文件中 – mssch 2015-04-03 15:55:52

+0

我刪除tomcat和mvn乾淨安裝,並重新部署它同樣的問題:( – tamtoum1987 2015-04-03 16:01:03

5

這裏是要建立一個實體管理器沒有彈簧和沒有persistence.xml一個純粹的編程方法。常量是從org.hibernate.cfg.AvailableSettings採取:

entityManagerFactory = new HibernatePersistenceProvider().createContainerEntityManagerFactory(
      archiverPersistenceUnitInfo(), 
      ImmutableMap.<String, Object>builder() 
        .put(JPA_JDBC_DRIVER, JDBC_DRIVER) 
        .put(JPA_JDBC_URL, JDBC_URL) 
        .put(DIALECT, Oracle12cDialect.class) 
        .put(HBM2DDL_AUTO, CREATE) 
        .put(SHOW_SQL, false) 
        .put(QUERY_STARTUP_CHECKING, false) 
        .put(GENERATE_STATISTICS, false) 
        .put(USE_REFLECTION_OPTIMIZER, false) 
        .put(USE_SECOND_LEVEL_CACHE, false) 
        .put(USE_QUERY_CACHE, false) 
        .put(USE_STRUCTURED_CACHE, false) 
        .put(STATEMENT_BATCH_SIZE, 20) 
        .build()); 

entityManager = entityManagerFactory.createEntityManager(); 

和臭名昭著的PersistenceUnitInfo

private static PersistenceUnitInfo archiverPersistenceUnitInfo() { 
    return new PersistenceUnitInfo() { 
     @Override 
     public String getPersistenceUnitName() { 
      return "ApplicationPersistenceUnit"; 
     } 

     @Override 
     public String getPersistenceProviderClassName() { 
      return "org.hibernate.jpa.HibernatePersistenceProvider"; 
     } 

     @Override 
     public PersistenceUnitTransactionType getTransactionType() { 
      return PersistenceUnitTransactionType.RESOURCE_LOCAL; 
     } 

     @Override 
     public DataSource getJtaDataSource() { 
      return null; 
     } 

     @Override 
     public DataSource getNonJtaDataSource() { 
      return null; 
     } 

     @Override 
     public List<String> getMappingFileNames() { 
      return Collections.emptyList(); 
     } 

     @Override 
     public List<URL> getJarFileUrls() { 
      try { 
       return Collections.list(this.getClass() 
              .getClassLoader() 
              .getResources("")); 
      } catch (IOException e) { 
       throw new UncheckedIOException(e); 
      } 
     } 

     @Override 
     public URL getPersistenceUnitRootUrl() { 
      return null; 
     } 

     @Override 
     public List<String> getManagedClassNames() { 
      return Collections.emptyList(); 
     } 

     @Override 
     public boolean excludeUnlistedClasses() { 
      return false; 
     } 

     @Override 
     public SharedCacheMode getSharedCacheMode() { 
      return null; 
     } 

     @Override 
     public ValidationMode getValidationMode() { 
      return null; 
     } 

     @Override 
     public Properties getProperties() { 
      return new Properties(); 
     } 

     @Override 
     public String getPersistenceXMLSchemaVersion() { 
      return null; 
     } 

     @Override 
     public ClassLoader getClassLoader() { 
      return null; 
     } 

     @Override 
     public void addTransformer(ClassTransformer transformer) { 

     } 

     @Override 
     public ClassLoader getNewTempClassLoader() { 
      return null; 
     } 
    }; 
} 

注意,Spring提供了配置持久精簡的方式,同時支持多種休眠版本。 (Spring 4.2支持Hibernate 5.1,Spring 4.3支持Hibernate 5.2)。

+0

太棒了!謝謝。 – Krismorte 2018-01-18 21:18:15

相關問題