0

Modelを拡張するクラスがあり、短いテキストとIDを保持しています。オブジェクトはデータベースに保存されます。

私の現在の問題は次のとおりです。古いバージョンのオブジェクトを置き換える代わりに、エントリを編集しようとするたびに新しいバージョンが作成されます。

私にとっては、保存する前にオブジェクトがコピーされたように見えますか?

誰かがこれを避ける方法を教えてくれたら、私はとてもうれしいです!:-)

モデル:

@Entity
public class Entry extends Model {

private static final long serialVersionUID = 1L;

@Required
public String   text;   
public String   studentName = "Hans";
@Id
public long     id;
public static   Finder<Long, Entry> finder = new Finder<Long, Entry>( Long.class, Entry.class);

public static Entry getMe(Long id) {
    return finder.byId(id);
}

public static List<Entry> getAll() {
    return finder.all();
}

public static void saveMe(Entry toDataBase) { 
    if(finder.byId(toDataBase.id)!=null)  // this seems to be always NULL 
    {
        System.out.println("update");
        toDataBase.update(); 
    }
    else
    {
        System.out.println("save new");
        toDataBase.save();
    }
}

コントローラー部分:

public static Result createReport(Long id) {
    if(Entry.getMe(id)==null)
        return ok(reports.render(form(Entry.class), Entry.getMe(id)));//Entry.class)));

    return ok(reports.render(entryForm.fill(Entry.getMe(id)),Entry.getMe(id)));//form(Entry.class), Entry.getMe(id)));//Entry.class)));
}
public static Result storeReport() {    
    Form<Entry> filledForm = entryForm.bindFromRequest();
    if(filledForm.hasErrors()) {
        return badRequest(error.render());
    } else {
        Entry.saveMe(filledForm.get());
        return ok(reports.render(filledForm, filledForm.get()));
    }
}

編集:html部分:

@(entryForm: Form[Entry],e: Entry)
@import helper._
@main("report creation") {
<h1>report for Nils</h1>

    @form(routes.Application.storeReport()) {
        @textarea(entryForm("text"))
        <input type="submit" value="Store Report">
    }
    <br />
    <br />
    @(if(e!=null)e.getid())
    @form(routes.Application.listStuff()) {
        <input type="submit" value="Overview">
    }
}
4

0 に答える 0