Playframework 从数据库编辑对象

Playframework 从数据库编辑对象,playframework,playframework-2.0,Playframework,Playframework 2.0,我有一个类扩展模型,包含一个简短的文本和id。对象存储在数据库中 我当前的问题是:每次尝试编辑条目时都会创建一个新版本,而不是替换对象的旧版本 对我来说,在保存之前,对象似乎被复制了 如果有人能告诉我如何避免这种情况,我将非常高兴!:-) 型号: @Entity public class Entry extends Model { private static final long serialVersionUID = 1L; @Required public String text;

我有一个类扩展模型,包含一个简短的文本和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();
    }
}
@实体
公共类入口扩展模型{
私有静态最终长serialVersionUID=1L;
@必需的
公共字符串文本;
公共字符串studentName=“Hans”;
@身份证
公共长id;
公共静态查找器=新查找器(Long.class、Entry.class);
公共静态条目getMe(长id){
返回finder.byId(id);
}
公共静态列表getAll(){
返回finder.all();
}
公共静态void saveMe(数据库项){
if(finder.byId(toDataBase.id)!=null)//这似乎总是null
{
System.out.println(“更新”);
toDataBase.update();
}
其他的
{
System.out.println(“保存新的”);
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()));
    }
}
公共静态结果createReport(长id){
if(Entry.getMe(id)==null)
返回ok(reports.render(form(Entry.class)、Entry.getMe(id));//Entry.class));
返回ok(reports.render(entryForm.fill(Entry.getMe(id)),Entry.getMe(id));//form(Entry.class),Entry.getMe(id));//Entry.class));
}
公共静态结果storeReport(){
Form filledForm=entryForm.bindFromRequest();
if(filledForm.hasErrors()){
返回badRequest(error.render());
}否则{
Entry.saveMe(filledForm.get());
返回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">
    }
}
@(entryForm:Form[Entry],e:Entry)
@导入助手_
@主(“报告创建”){
报告为零
@表单(routes.Application.storeReport()){
@文本区域(入口形式(“文本”))
}


@(如果(e!=null)e.getid()) @表单(routes.Application.listStuff()){ } }
尝试使用
Long
而不是
Long
。并使用getter和setter而不是公共字段。感谢您的建议!不幸的是,它仍然不起作用:(…但您是对的-getter和setter比公共字段更酷;)您可以尝试定义finder,比如
publicstaticfinder find=newfinder(String.class,Product.class)
?我也更喜欢使用getter和setter。您是否更改了id的Long类型:
公共Long id;
如果是,请尝试
清理
然后
运行
您的项目。在使用公共道具时,这对我来说是一个节约。是的,我更改为Long,在getter和setter中也是如此。在finder中,我无法-就是这样仍然很长(…不是字符串,因为我希望能够在数据库中多次使用相同的文本)。我将HTWL部分集成到我的帖子中-问题可能就在那里?但无论如何,我认为我将通过使用不同的框架实现项目来“解决”问题:(非常感谢您的帮助!)