【问题标题】:hibernate implementation with play framework 2.3x使用 play framework 2.3x 实现休眠
【发布时间】:2015-09-18 07:49:17
【问题描述】:

我正在使用 play framework 2.3x,我使用 play framework 实现了休眠,但我在控制器类的会话工厂中获得了 null 值。实际上这个 sessionFactory 没有得到 hibernate-cfg.xml 文件。 请偷偷摸摸

我的 build.sbt 是

 libraryDependencies ++= Seq(
  javaJdbc,
  javaEbean,
  cache,
  javaWs,
  "mysql" % "mysql-connector-java" % "5.1.18",
  javaJpa.exclude("org.hibernate.javax.persistence", "hibernate-jpa-2.0-api"),
  "org.hibernate" % "hibernate-entitymanager" % "4.3.9.Final",
  "com.google.code.gson" % "gson" % "2.2"
)

我的 conf/Hibernate-cfg.xml 是

  <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.dialect">
                org.hibernate.dialect.MySQLDialect
            </property>
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost/test</property>
            <property name="connection.username">root</property>
            <property name="connection.password"></property>
            <property name="connection.pool_size">10</property>
            <property name="current_session_context_class">thread</property>
            <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
            <property name="show_sql">true</property>
            <property name="hbm2ddl.auto">validate</property>


        </session-factory>
    </hibernate-configuration>
**My Model class is**
@javax.persistence.Entity
@Table(name = "customer")
public class Customer extends Model {
    public static Finder<Integer, Customer> find = new Finder<Integer, Customer>(
            Integer.class, Customer.class
    );

    @Id
    @Column(name = "cid")
    int cid;
    @Column(name = "name")
    public String name;

    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static List<Customer> findAll() {
        return find.all();
    }
}

【问题讨论】:

    标签: java spring hibernate playframework playframework-2.0


    【解决方案1】:

    您应该将您的 JPA 配置放在 conf/META-INF/persistance.xml 中。这是 JPA 的标准位置,而 Hibernate 只是 JPA 的一个实现。 Play 在conf/application.conf 中有几个额外的配置。 Integrating Play with JPA 有一个很好的教程。

    另外一点:你在你的控制器方法中使用@Transactional注解吗?如果不是,您必须明确告诉 Play 手动使用 JPA.withTransaction 启动事务。

    【讨论】:

      猜你喜欢
      • 2016-12-12
      • 2014-05-16
      • 1970-01-01
      • 2016-12-14
      • 2015-01-19
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多