Eduardo
Eduardo

Reputation: 3

Play + JPA + Hibernate + PostgreSQL: Cannot create Tables

I've been trying to create a simple and small application using Play Framework 2.4.0, JPA, and Hibernate 4.3.9 to connect to a PostgreSQL 9.4 database. I want the application to create new database tables based on the models created on the application. So far, I've had no success.

This is the content of my "build.sbt":

name := """aplicacion1"""

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayJava)

scalaVersion := "2.11.6"

libraryDependencies ++= Seq(
  javaJdbc,
  cache,
  javaJpa,
  //"org.apache.directory.api" % "apache-ldap-api" % "1.0.0-M14",
  "postgresql" % "postgresql" % "9.1-901-1.jdbc4",
  "org.hibernate" % "hibernate-core" % "4.3.9.Final",
  "org.hibernate" % "hibernate-entitymanager" % "4.3.9.Final"

)

// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
routesGenerator := InjectedRoutesGenerator

This is the content of my "application.conf" describing the database conection:

db.default.driver=org.postgresql.Driver
db.default.url="postgres://postgres:passsuper@localhost/tesis_play1"
db.default.jndiName=DefaultDS
jpa.default=defaultPersistenceUnit

This is the content of my "persistence.xml":

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">

    <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <non-jta-data-source>DefaultDS</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>

</persistence>

And this is the small model class I'm trying to get into the database:

@Entity
public class ruta {

    @Id
    public int idRuta;
    public String nombre;
    public boolean esIda;
    public boolean esvuelta;
    public String apodo1;
    public String apodo2;
    public String ref;

}

The application compiles and runs with no errors, but the "ruta" table is never created on the database. Is there something I'm missing? Thanks for the help!

EDIT: The following appears on the console when accessing the application through "localhost:9000":

[info] Compiling 2 Java sources to C:\Users\Enrique\Desktop\tesis_2\aplicacion1\target\scala-2.11\classes...
SLF4J: The following set of substitute loggers may have been accessed
SLF4J: during the initialization phase. Logging calls during this
SLF4J: phase were not honored. However, subsequent logging calls to these
SLF4J: loggers will work as normally expected.
SLF4J: See also http://www.slf4j.org/codes.html#substituteLogger
SLF4J: org.webjars.CloseQuietly
[info] - application - Creating Pool for datasource 'default'
[info] - play.api.db.HikariCPConnectionPool - datasource [jdbc:postgresql://localhost/tesis_play1] bound to JNDI as DefaultDS
[info] - play.api.db.DefaultDBApi - Database [default] connected at jdbc:postgresql://localhost/tesis_play1
[info] - play.api.libs.concurrent.ActorSystemProvider - Starting application default Akka system: application
[info] - play.api.Play$ - Application started (Dev)

Upvotes: 0

Views: 1636

Answers (2)

Pawel Hawro
Pawel Hawro

Reputation: 1536

Generated hbm2ddl database changes are called when Hibernate SessionFactory is created. Starting application does not create EntityManagerFactory and underlying Hibernate SessionFactory.

Changes on database will be made on first call to JPA, when EntityManagerFactory is created. To start EntityManagerFactory when your app starts, just create Global class with JPA call:

public class Global extends GlobalSettings {

    public void onStart(Application app) {

        JPA.withTransaction(new F.Callback0() {
            @Override
            public void invoke() throws Throwable {
                play.Logger.debug("First JPA call");
            }
        });


    }

    public void onStop(Application app) {
    }

}

Upvotes: 2

jsonmurphy
jsonmurphy

Reputation: 1600

Perhaps you could try to add this to your persistence.xml:

....
<property name="hibernate.archive.autodetection" value="class, hbm"/>
...

I'm no JPA expert myself but this seems like the only difference between your persistence.xml and the one I have in a project of mine. According to the docs this property will:

Determine which element is auto discovered by Hibernate Entity Manager while parsing the .par archive. (default to class,hbm).

It says it should default to class,hbm but I'd still try anyway. Outside of that the only other thing I could recommend is to try to add the database driver and credentials in the persistence.xml like:

<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
<property name="hibernate.connection.password" value="foobar"/>
<property name="hibernate.connection.url" value="jdbc:h2:mem:play"/>
<property name="hibernate.connection.username" value="sa"/>

Upvotes: 0

Related Questions