简体   繁体   English

播放框架db.play.JPA

[英]Play Framework db.play.JPA

Okay. 好的。 I am having big confusion with Play Framework. 我对Play Framework感到困惑。 (Do they update their tutorial and documentation as often as their products?). (他们是否会像他们的产品一样经常更新其教程和文档?)。 I am trying to do the tutorial for CRUD app, and I am using JPA instead of Ebean (because the last one does not work, but, as appeared, the first either). 我正在尝试为CRUD应用程序编写教程,并且我使用的是JPA而不是Ebean(因为最后一个不起作用,但如出现的那样,第一个也不起作用)。 Compiler says, it can not find class Model. 编译器说,它找不到类Model。

Internet offers to get the right dependencies and I already tried a lot of them. Internet提供了正确的依赖关系,我已经尝试了很多。 But I can't get the right one. 但是我找不到合适的人。 Can anyone help me how to make this easiest thing work? 谁能帮我如何使这件最简单的事情起作用?

Here are my routes: 这是我的路线:

# Home page
GET     /                           controllers.Application.index()
POST    /person               controllers.Application.addPerson() 

Here are the app configuration, that were oncommented 这是已注释的应用程序配置

db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"

built.sbt dependences: Built.sbt依赖关系:

libraryDependencies ++= Seq(
  Jdbc, 
  javaJpa,
  "org.hibernate" % "hibernate-entitymanager" % "4.3.5.Final"
  )

Application.java: Application.java:

package controllers;

import models.Person;
import play.*;
import play.data.Form;
import play.mvc.*;

import views.html.*;
public class Application extends Controller {

    public Result index() {
        return ok(index.render("Your new application is ready."));
    }

    public Result addPerson(){
        Person person = Form.form(Person.class).bindFromRequest().get();
        person.save();
        return redirect(routes.Application.index());
    }

}

And models Person.java: 并建模Person.java:

package models;

import java.util.*;
import javax.persistence.*;
 import play.db.jpa.*;

@Entity 
public class Person extends Model{
    @Id
    public String id;
    public String name;


}

By using JPA without Ebean it should be like described here For example (haven't tested actually) like fallowing code: 通过在不使用Ebean的情况下使用JPA,应该像此处描述的那样例如(尚未经过实际测试)类似以下代码:

public Result addPerson(){
    Person person = Form.form(Person.class).bindFromRequest().get();
    EntityManager em = JPA.em();
    em.persist(person);
    em.flush();
    return redirect(routes.Application.index());
}

and model will look similar to that: 和模型将类似于以下内容:

@Entity 
public class Person
{

    private long id;
    private String name;

    @Id
    @Column(name = "id")
    public long getId()
    {
        return id;
    }

    public void setId(long id)
    {
        this.id = id;
    }

    @Basic
    @Column(name = "name")
    public String getName()
    {
        return name;
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM