简体   繁体   中英

Why does my Play framework model save to the database as a blank record?

I am fairly new to Java and working on a web-based project using the Play framework. It's been a great learning experience and I've been able to find solutions to most of problems, but I'm stuck on saving a model to the database.

I am using framework version 2.3.4 with a postgresql database running locally. The connection is fine, but when I use the .save() method on my object(Newspaper) a blank record is saved with only the ID field having any data. The form seems to have the values binded by the request just fine. Why can't this be saved?

I've searched for answers, but it doesn't seem like anyone has had this issue.

I have noticed that the ID generated by the sequence doesn't always use the next number (skips 20 or so) even though I'm running locally and the only user?? That may be a clue for someone...

Here's the model:

package models;

import java.util.*;
import javax.persistence.*;

import play.mvc.*;

import play.db.ebean.Model;
import play.data.validation.*;

/**
 * Newspaper entity managed by Ebean
 */
@Entity
public class Newspaper extends Model {

private static final long serialVersionUID = -4004723260163933009L;

@Id
@GeneratedValue(generator="newspaper_seq")
@SequenceGenerator(name="newspaper_seq", allocationSize=1)
public Long id;

public String name;

public Long rate;

/**
 * Generic query helper for entity Newspaper with id Long
 */
public static Model.Finder<Long,Newspaper> find = new Model.Finder<Long,Newspaper>(Long.class, Newspaper.class);

public static Map<String,String> options() {
    LinkedHashMap<String,String> options = new LinkedHashMap<String,String>();
    for(Newspaper c: Newspaper.find.orderBy("name").findList()) {
        options.put(c.id.toString(), c.name);
    }
    return options;
}

}

And my controller class:

package controllers;


import play.*;

import play.mvc.*;


import views.html.*;

import models.Newspaper;

//import play.api.data.Form; 

import play.data.Form;

public class Application extends Controller{

/**
 * @param args
 */
public static Result index() {
    return ok(views.html.index.render("list of newspapers"));

}

public static Result addNewspaper() {
    Form<Newspaper> form = Form.form(Newspaper.class).bindFromRequest();
    //return ok(form.toString()); 
    if(form.hasErrors()) {
        return badRequest("something didn't work");
    }
    String oldForm = form.toString();
    Newspaper paper = form.get();
    paper.save();


// checking value        return ok(oldForm + "\n" + form.toString() + "\n" + paper.toString());
  return redirect(routes.Application
          .index()); 
}


}

And here is my SQL to create the table (first time using postgresql so this could be wrong)

create table newspaper (
id                        bigint not null,
name                      varchar(255),
rate                      bigint,
constraint pk_newspaper primary key (id))
;

create sequence newspaper_seq;

Thank you for your time!

---Update--- I started sql debugging and I saw this error in the terminal :

[debug] c.j.b.PreparedStatementHandle - insert into newspaper (id, name, rate) values (101,'[SQL NULL of type 12]','[SQL NULL of type -5]')

For some reason, the values are NULL (of different types?), but when I output the .toString value of the form it seems to show the correct data:

Form(of=class models.Newspaper, data={name=Toledo Blade, rate=10}, value=Some(models.Newspaper@2df64761), errors={})

Any reason why the data wouldn't be passed to the Newspaper model?

OK- so I found the answer on another question . I guess I wasn't using the right search terms :( Anyway, I had to type activator clean from the project directory in terminal. After that it worked fine. Hopefully this can save someone else hours of frustration!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM