저는 플레이 프레임 워크의 초보자이며 폼 데이터를 두 개의 다른 테이블에 저장하는 데 어려움이 있습니다.OneToMany 관계 및 플레이 프레임 워크를 사용한 폼 저장
양식을 제출하면 이름, 성 및 텍스트가 데이터베이스에 저장되지만 학교 이름이 학교 테이블에 저장되지 않는 이유를 찾을 수 없습니다. 튜토리얼을보고 다른 사람들의 샘플 코드를 살펴보고 해결책을 찾지 못했습니다. 제발 누군가가 나를 도울 수 있다면 정말 고맙겠습니다.
후보 모델 :
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");
}