2014-04-09 123 views
0

我試圖獲取配置Hibernate與我的Scala代碼的工作,當我去運行應用程序,我得到如下:沒有持久性提供的EntityManager

javax.persistence.PersistenceException: No Persistence provider for EntityManager named studentalerts 
    javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61) 
    hibernateconfig.HibernateConfig$.getEntityManager(HibernateConfig.scala:29) 
    hibernateconfig.HibernateConfig$.createEntityManager(HibernateConfig.scala:11) 
    controller.test.<init>(test.scala:8) 
    controller.TestHome.doGet(register.scala:11 

我的persistence.xml文件(在我的WEB -INF /班/ META-INF文件夾如下

<persistence-unit name="studentalerts" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
    <non-jta-data-source>jdbc/studentalerts</non-jta-data-source> 
    <properties> 
    </properties> 
</persistence-unit> 

和我的相關Scala代碼是

object HibernateConfig { 
    var mapping: SearchMapping = _ 
    def createEntityManager(): EntityManager = getEntityManager 
    def getEntityManager: EntityManager = { 
    val properties: Properties = new Properties() 

    properties.put("javax.persistence.provider", "org.hibernate.jpa.HibernatePersistenceProvider") 
    properties.put("javax.persistence.transactionType", "RESOURCE_LOCAL") 
    properties.put("javax.persistence.jdbc.url", "jdbc:mysql://localhost:3306/studentalerts") 
    properties.put("javax.persistence.jdbc.user", "alert") 
    properties.put("javax.persistence.jdbc.password", "12345") 
    properties.put("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver") 
    properties.put("hibernate.hbm2ddl.auto", "update") 
    properties.put("hibernate.show_sql", "false") 
    properties.put("hibernate.format_sql", "false") 
    properties.put("hibernate.current_session_context_class", "thread") 
    properties.put("hibernate.transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory") 
    properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect") 

    val emf: EntityManagerFactory = Persistence.createEntityManagerFactory("studentalerts", properties) 
    emf.createEntityManager() 
    } 
} 

它具有硬時間線與

val emf: EntityManagerFactory = Persistence.createEntityManagerFactory("studentalerts", properties) 

任何幫助不勝感激。

感謝

回答

0

provider包聲明似乎不正確。

<provider>org.hibernate.jpa.HibernatePersistenceProvider<provider> 

嘗試更改配置,如下所示。

<provider>org.hibernate.ejb.HibernatePersistence</provider> 

此外,你正在混合這兩個東西,配置編程+ xml。它應該以任何方式完成。

只能通過設置所有屬性&來使用它,如下所示通過xml進行配置。

EntityManagerFactory = Persistence.createEntityManagerFactory("studentalerts"); 

Persistence.xml - 相應地添加屬性。

<?xml version="1.0" encoding="UTF-8"?> 
<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" 
     version="2.0"> 
    <persistence-unit name="studentalerts"> 
     <properties> 
      <property name="javax.persistence.jdbc.driver" value="XYZ"/> 
      <property name="javax.persistence.jdbc.url" value="XYZ"/> 
      <property name="hibernate.show_sql" value="true"/> 
     </properties> 
    </persistence-unit> 
</persistence> 
相關問題