我是 Play Framework 的初学者,在将表单数据保存到两个不同的表时遇到了一些困难。
当我提交表单时,名字、姓氏和文本被保存到数据库中,但我似乎无法找出为什么 schoolName 不会保存到学校表中。我花了很多时间看教程和浏览其他人的示例代码,但还没有看到解决方案。如果有人可以帮助我,我将不胜感激。
这是我所拥有的:
候选型号:
package models;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import play.db.ebean.Model;
@Entity
public class Candidate extends Model {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
public String firstName;
public String lastName;
public boolean sponsorshipStatus;
public boolean proceedToInterview;
public String text;
@OneToMany(mappedBy="candidate", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private List<School> schools = new ArrayList<School>();
/**
* Generic query helper for entity Candidate with id Long
*/
public static Model.Finder<Long, Candidate> find = new Model.Finder<Long, Candidate>(
Long.class, Candidate.class);
}
学校模式:
package models;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import play.db.ebean.Model;
@Entity
public class School extends Model {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
public String schoolName;
@ManyToOne(cascade=CascadeType.ALL)
public Candidate candidate = new Candidate();
/**
* Generic query helper for entity Company with id Long
*/
public static Model.Finder<Long, School> find = new Model.Finder<Long, School>(
Long.class, School.class);
}
表格模板:
@(candidateForm: Form[Candidate])
@import helper._
@main("Create Candidate"){
<h1>Add a Candidate</h1>
@form(routes.Application.save()) {
<fieldset>
${candidateForm("firstName")}
@inputText(candidateForm("firstName"), '_label -> "First Name")
@inputText(candidateForm("lastName"), '_label -> "Last Name")
@inputText(candidateForm("text"), '_label -> "Text")
@inputText(candidateForm("schoolName"), '_label -> "School")
</fieldset>
<div class="actions">
<input type="submit" value="Create this computer" class="btn primary"> or
<a href="" class="btn">Cancel</a>
</div>
}
}
控制器:
package controllers;
import static play.data.Form.form;
import models.Candidate;
import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.createForm;
import views.html.index;
public class Application extends Controller {
public static Result index() {
return ok(index.render("Your new application is ready."));
}
/**
* Display the 'new candidate form'.
*/
public static Result create() {
Form<Candidate> candidateForm = form(Candidate.class);
return ok(views.html.createForm.render(candidateForm));
}
/**
* Handle the 'new computer form' submission
*/
public static Result save() {
Form<Candidate> candidateForm = form(Candidate.class).bindFromRequest();
if(candidateForm.hasErrors()) {
return badRequest(createForm.render(candidateForm));
}
candidateForm.get().save();
flash("success", "Computer " + candidateForm.get().firstName + " has been created");
return redirect("/candidates/new");
}