Update the Task model - RuntimeException: DataSource user is null?

北战南征 提交于 2019-12-10 10:37:54

问题


I started learning Play framework today and it is very good and easy to learn.

I successfully completed the sample provided on their website, but I wanted to make some modifications to it.

I wanted to see if could update the label of a particular task, so I followed the following approach

First I added a route to update the data

POST    /tasks/:id/update           controllers.Application.updateTask(id: Long)

Then I added the following code to index.scala.html file

 @form(routes.Application.updateTask(task.id)) {
                    <label class="contactLabel">Update note here:</label> 
 @inputText(taskForm("label")) <br />
                }

Then I modified Application.java class to

public static Result updateTask(Long id) {
        Form<Task> taskForm = Form.form(Task.class).bindFromRequest();
        if (taskForm.hasErrors()) {
            return badRequest(views.html.index.render(Task.all(), taskForm));
        } else {
            Task.update(id, taskForm.get());
            return redirect(routes.Application.tasks());
        }
    }

Finally in Task.java I added this code

public static void update(Long id, Task task) {
        find.ref(id).update(task.label);
    }

But when I perform the update operation I get this error

[RuntimeException: DataSource user is null?]

Needless to say that I commented out

 db.default.driver=org.h2.Driver
 db.default.url="jdbc:h2:mem:play"
 ebean.default="models.*"

in application.conf since I am already able to save and delete data; but I cannot update the data in the database, why is this happening, did someone try this before, how can I solve this error?


回答1:


Your update(Long id, Task task) method on Task model should be like below:

public static void update(Long id, Task task) {
    task.update(id); // updates this entity, by specifying the entity ID
}

Because you passed task variable as updated data, you don't need to find reference of Task object like you do in find.ref(id). And moreover, update() method on play.db.ebean.Model class (with one parameter) needs ID of model as parameter.

Hope this help solving your problem.. :)




回答2:


Ensure you have commented out Ebean configuration in application.conf

# Ebean configuration
# ~~~~~
# You can declare as many Ebean servers as you want.
# By convention, the default server is named `default`
#
ebean.default="models.*"


来源:https://stackoverflow.com/questions/16122884/update-the-task-model-runtimeexception-datasource-user-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!