MaxPower
MaxPower

Reputation: 871

No Persistence provider for EntityManager

I am trying get configure Hibernate to work with my Scala code, and when I go to run the application I get the following:

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

My persistence.xml file (in my WEB-INF/classes/META-INF folder is as follows

<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>

and my relevant Scala code is

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()
  }
}

The line it is having a hard time is with

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

Any help is greatly appreciated.

Thanks

Upvotes: 0

Views: 556

Answers (1)

Nayan Wadekar
Nayan Wadekar

Reputation: 11622

The package declaration for provider seems incorrect.

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

Try changing the configuration as below.

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

Also, you are mixing both the things, configuring programmatically + xml. It should be done either ways.

Configure through xml only by setting all properties & use it, like below.

EntityManagerFactory = Persistence.createEntityManagerFactory("studentalerts");

Persistence.xml - add properties accordingly.

<?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>

Upvotes: 0

Related Questions