Play 2.2 with Hibernate JPA and Postgres Play 2.2 with Hibernate JPA and Postgres postgresql postgresql

Play 2.2 with Hibernate JPA and Postgres


You said that you didn't write any code, so I decided to show you how I created new Play! 2.2 application using JPA and Postgresql. You can do the same and check the difference.

First I created new Play application with command:

play new testApp

Then I created persistence.xml file in testApp/conf/META-INF directory and fill it with content:

<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="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">    <provider>org.hibernate.ejb.HibernatePersistence</provider>    <non-jta-data-source>DefaultDS</non-jta-data-source>    <properties>        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>        <!--<property name="hibernate.show_sql" value="true"/>-->        <property name="hibernate.hbm2ddl.auto" value="update"/>        <property name="hibernate.format_sql" value="true"/>    </properties></persistence-unit>

Added to my testApp/conf/application.conf:

jpa.default=defaultPersistenceUnitdb.default.driver=org.postgresql.Driverdb.default.url="postgres://postgres:postgres@localhost/test"# You can expose this datasource via JNDI if needed (Useful for JPA)db.default.jndiName=DefaultDS

I also created sample model class:

@Entity@SequenceGenerator(name = "Token_generator", sequenceName = "test_sequence", allocationSize = 1, initialValue = 1)public class Test {    @Id    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Token_generator")    public Long id;    public String name;}

I started play app with command:

play ~run

Then I was able to see working website under http: //localhost:9000/ address.I was also able to see new Table test in postgres test database.


Ok, figured it out by myself. It was indeed a problem with two versions of xml-apis. One coming from Play itself and one from the Apache Directory API. I changed the artefact in the build.sbt to and it is working now:

  "org.apache.directory.api" % "api-all" % "1.0.0-M14"