Note that there are some explanatory texts on larger screens.

plurals
  1. POediting an object from the database
    text
    copied!<p>I have a class extending Model, holding a short text and id. Objects are stored in a database.</p> <p>My current problem is: Instead of replacing the old version of an object, a new one is created every time I try to edit an entry.</p> <p>For me it looks as if the object is copied somewhen before saving?</p> <p>I'd be very glad, if somebody could tell me how to avoid this! :-)</p> <p>Model:</p> <pre><code>@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&lt;Long, Entry&gt; finder = new Finder&lt;Long, Entry&gt;( Long.class, Entry.class); public static Entry getMe(Long id) { return finder.byId(id); } public static List&lt;Entry&gt; 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(); } } </code></pre> <p>The controller part:</p> <pre><code>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&lt;Entry&gt; filledForm = entryForm.bindFromRequest(); if(filledForm.hasErrors()) { return badRequest(error.render()); } else { Entry.saveMe(filledForm.get()); return ok(reports.render(filledForm, filledForm.get())); } } </code></pre> <p>edit: the html part:</p> <pre><code>@(entryForm: Form[Entry],e: Entry) @import helper._ @main("report creation") { &lt;h1&gt;report for Nils&lt;/h1&gt; @form(routes.Application.storeReport()) { @textarea(entryForm("text")) &lt;input type="submit" value="Store Report"&gt; } &lt;br /&gt; &lt;br /&gt; @(if(e!=null)e.getid()) @form(routes.Application.listStuff()) { &lt;input type="submit" value="Overview"&gt; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload