无法使用 Hibernate 配置 Play 2.2 应用程序
Not able to configure Play 2.2 app with Hibernate
我正在尝试使用休眠设置 play 2.2 应用程序,但它似乎无法正常工作。我想我拥有所有正确的文件,但是当我加载我的应用程序时,table 不会像使用 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
将各种文件更改为上述文件后,我制作了一个简单的模型来测试它是否设置正确以及休眠是否会自动生成 table,但似乎没有任何反应
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 数据库和 table 的 none 已经创建,并且已经创建了进化脚本。还有什么我需要 do/do 我需要手动创建进化脚本吗?我以为它会像 E-Bean 一样自动创建。
从您的 persistence.xml 中删除 "Resource local" 在容器管理环境的情况下,我们不使用本地资源。默认情况下它使用 JTA 事务。所以你可以删除 "Resource local " 并保留它。
您的 persistance.xml
中缺少 hbm2ddl
属性:
<property name="hibernate.hbm2ddl.auto" value="update" />
可能的值为:create
、update
、create-drop
、validate
。更多 details here.
我正在尝试使用休眠设置 play 2.2 应用程序,但它似乎无法正常工作。我想我拥有所有正确的文件,但是当我加载我的应用程序时,table 不会像使用 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
将各种文件更改为上述文件后,我制作了一个简单的模型来测试它是否设置正确以及休眠是否会自动生成 table,但似乎没有任何反应
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 数据库和 table 的 none 已经创建,并且已经创建了进化脚本。还有什么我需要 do/do 我需要手动创建进化脚本吗?我以为它会像 E-Bean 一样自动创建。
从您的 persistence.xml 中删除 "Resource local" 在容器管理环境的情况下,我们不使用本地资源。默认情况下它使用 JTA 事务。所以你可以删除 "Resource local " 并保留它。
您的 persistance.xml
中缺少 hbm2ddl
属性:
<property name="hibernate.hbm2ddl.auto" value="update" />
可能的值为:create
、update
、create-drop
、validate
。更多 details here.