没有EntityManager的持久性提供程序

时间:2014-04-09 02:45:40

标签: hibernate scala jpa

我正在尝试配置Hibernate以使用我的Scala代码,当我去运行应用程序时,我得到以下内容:

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 / classes / 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)

非常感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:0)

provider的包声明似乎不正确。

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

尝试更改配置,如下所示。

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

此外,您正在混合两者,以编程方式配置+ xml。它应该以任何一种方式完成。

仅通过设置所有属性&amp;使用它,如下所示。

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>