使用Hibernate JPA和Postgres玩2.2

lin*_*ips 3 postgresql hibernate jpa playframework playframework-2.0

我正在尝试使用Play 2.2项目来使用Hibernate JPA和PostgreSQL数据库.我之前使用Play 2.1.1,它完美地工作.我现在收到以下错误:

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]
Run Code Online (Sandbox Code Playgroud)

我不知道这是从哪里来的.我的build.sbt看起来像这样:

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"
)    
Run Code Online (Sandbox Code Playgroud)

我的persistence.xml像这样:

<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>
Run Code Online (Sandbox Code Playgroud)

我还没有编写任何代码,我只是配置它.

ban*_*dit 6

你说你没有写任何代码,所以我决定告诉你我是如何创建新游戏的!2.2使用JPA和Postgresql的应用程序.您也可以这样做并检查差异.

首先,我用命令创建了新的Play应用程序:

play new testApp
Run Code Online (Sandbox Code Playgroud)

然后我在testApp/conf/META-INF目录中创建了persistence.xml文件并用内容填充它:

<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>
Run Code Online (Sandbox Code Playgroud)

添加到我的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
Run Code Online (Sandbox Code Playgroud)

我还创建了示例模型类:

@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;
}
Run Code Online (Sandbox Code Playgroud)

我用命令开始玩app:

play ~run
Run Code Online (Sandbox Code Playgroud)

然后我能够在http:// localhost:9000/address下看到工作网站.我还能在postgres测试数据库中看到新的Table测试.