Play 2.2 with Hibernate JPA and Postgres

2019-04-10 22:50发布

I'm trying to get a Play 2.2 project to work with Hibernate JPA and a PostgreSQL database. I did it before with Play 2.1.1, where it worked perfectly. I get the following error now:

play.api.UnexpectedException: Unexpected exception[NoClassDefFoundError: org/w3c/dom/ElementTraversal]
    at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:152) ~[play_2.10.jar:2.2.0]

I have no idea where this comes from. My build.sbt looks like this:

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.2.3.Final",
  "org.hibernate" % "hibernate-entitymanager" % "4.2.3.Final"
)    

And my persistence.xml like this:

<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.hbm2ddl.auto" value="update" />
    </properties>
</persistence-unit>

I haven't written any code yet, I just configured it.

3条回答
男人必须洒脱
2楼-- · 2019-04-10 22:59

I narrowed it further down by myself. It seams that it is caused a conflict caused by the org.apache.directory.api I'm using. It is working without this library. I get the following message from sbt:

[error] Relocation to an other version number not supported in ivy : xml-apis#xml-apis;2.0.2 relocated to xml-apis#xml-apis;1.0.b2
 . Please update your dependency to directly use the right version.

But I have no idea how to solve this conflict.

查看更多
对你真心纯属浪费
3楼-- · 2019-04-10 23:05

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"
查看更多
霸刀☆藐视天下
4楼-- · 2019-04-10 23:09

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=defaultPersistenceUnit
db.default.driver=org.postgresql.Driver
db.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.

查看更多
登录 后发表回答