2

私は Play フレームワークの初心者で、フォーム データを 2 つの異なるテーブルに保存するのに苦労しています。

フォームを送信すると、firstName、lastName、およびテキストがデータベースに保存されますが、schoolName がschools テーブルに保存されない理由がわかりません。チュートリアルを見たり、他の人のサンプルコードを調べたりするのにかなりの時間を費やしましたが、解決策を見たことはありません. 誰かが私を助けてくれれば、本当に感謝しています。

ここに私が持っているものがあります:

候補モデル:

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");
    }
4

3 に答える 3