【问题标题】:Not able to configure Play 2.2 app with Hibernate无法使用 Hibernate 配置 Play 2.2 应用程序
【发布时间】:2015-01-22 06:12:04
【问题描述】:

我正在尝试使用 hibernate 设置 play 2.2 应用程序,但它似乎无法正常工作。我认为我拥有所有正确的文件,但是当我加载我的应用程序时,这些表不会像使用 EBEAN 时那样自动生成。这是我的文件:

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.H2Dialect"/>
        </properties>
    </persistence-unit>

</persistence>

build.sbt

name := "listr"

version := "1.0-SNAPSHOT"


libraryDependencies ++= Seq(
  javaJdbc,
  javaEbean,
  cache,
  javaJpa,
  "org.hibernate" % "hibernate-core" % "4.2.3.Final",
  "org.hibernate" % "hibernate-entitymanager" % "4.2.3.Final"
)     

val appDependencies = Seq(
  javaJdbc
)

play.Project.playJavaSettings

conf/application.conf

# Database configuration
# ~~~~~
# You can declare as many datasources as you want.
# By convention, the default datasource is named `default`
#
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
# db.default.user=sa
# db.default.password=""
db.default.jndiName=DefaultDS
jpa.default=defaultPersistenceUnit

将各种文件更改为上述文件后,我制作了一个简单的模型来测试它是否设置正确以及hibernate是否会自动生成表格,但似乎什么也没发生

apps/models/List.java

package models;

import javax.persistence.Entity;
import javax.persistence.Id;


@Entity
public class List {
    @Id
    private long id;
    private String name;

}

创建此模型后,我运行了项目,加载后检查了 h2 数据库,没有创建任何表,也没有创建进化脚本。还有什么我需要做的/我需要手动创建进化脚本吗?我认为它会像使用 E-Bean 一样自动创建。

【问题讨论】:

    标签: java hibernate orm playframework playframework-2.0


    【解决方案1】:

    您的persistance.xml 中缺少hbm2ddl 属性:

    <property name="hibernate.hbm2ddl.auto" value="update" />
    

    可能的值为:create、update、create-drop、validate。更多details here.

    【讨论】:

      【解决方案2】:

      从您的 persistence.xml 中删除“本地资源”在容器管理环境的情况下,我们不使用本地资源。默认情况下,它使用 JTA Transaction。这样您就可以删除“本地资源”并保留它。

      【讨论】:

        猜你喜欢
        • 2015-05-05
        • 1970-01-01
        • 1970-01-01
        • 2014-06-23
        • 1970-01-01
        • 2013-09-29
        • 2019-08-12
        • 2021-08-03
        • 1970-01-01
        相关资源
        最近更新 更多