Skip to content

Play! And Hibernate – How To Get Running With Your JSON REST API

Now that our resources are planned let’s quickly set up Play! for persistence. I don’t like configuration… it kicks me out of my workflow and makes me thinking if to continue my work because the next step would be… yeah… configuring something.

Nevertheless with Play it is not that bad and done really quick. You will be able to persist Java classes to a in-memory H2 database using JPA.

1. application.conf

application_conf

Add DB driver, JNDI settings, evolutions which will care about your db schema and name your default persistence unit.

 

2. Build.scala

build_scala

Add the hibernate dependencies to Build.scala. Additionally you just need javaCore and javaJpa. Quite slim.

In case you don’t know what is the most current version for the entity manager library then check out this site and click on the sbt tab.

 

3. persistence.xml

persistence_xml

 

Here we map application.conf setting for the DefaultDS and set the correct SQL dialect. We want a very chatty configuration and automatic schema updates.

4. Done

Exit play and load your application again.

The upcoming post in this series will describe how to use persistency for a hierarchical model with an abstract base class.

 

2 Comments

  1. Sergii Oliinyk wrote:

    I did exactly the same thing as you posted but PersistenceException: No Persistence provider for EntityManager named defaultPersistenceUnit, i would be very thenkfull for the help. I just copy pasted the same things you have

    Wednesday, November 27, 2013 at 11:38 AM | Permalink
  2. Patrick wrote:

    Hello Sergii,

    can you let me know more? It seems like this is a problem with your persistence.xml.
    Please check that the jndi name in your application.conf
    db.default.jndiName=DefaultDS
    equals to the setting in your persistence.xml
    DefaultDS

    You could send me your application.conf and persistence.xml and I take a look to patrick@kerkermeister.net.
    Which version of play are you using?

    Thanks a lot,
    Patrick

    Wednesday, November 27, 2013 at 7:52 PM | Permalink