更新任务模型 – RuntimeException:DataSource用户是否为空?
我今天开始学习Play框架,它非常好且易于学习。
我成功完成了他们网站上提供的示例,但我想对其进行一些修改。
我想看看是否可以更新特定任务的标签,所以我遵循以下方法
首先,我添加了一条更新数据的路径
POST /tasks/:id/update controllers.Application.updateTask(id: Long)
然后我将以下代码添加到index.scala.html
文件中
@form(routes.Application.updateTask(task.id)) { @inputText(taskForm("label"))
}
然后我修改了Application.java类
public static Result updateTask(Long id) { Form 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()); } }
最后在Task.java中我添加了这段代码
public static void update(Long id, Task task) { find.ref(id).update(task.label); }
但是当我执行更新操作时,我收到此错误
[RuntimeException:DataSource用户为空?]
不用说我评论了
db.default.driver=org.h2.Driver db.default.url="jdbc:h2:mem:play" ebean.default="models.*"
在application.conf中,因为我已经能够保存和删除数据; 但我无法更新数据库中的数据,为什么会发生这种情况,之前有人试过这个,我该如何解决这个错误呢?
Task
模型上的update(Long id, Task task)
方法应如下所示:
public static void update(Long id, Task task) { task.update(id); // updates this entity, by specifying the entity ID }
因为您将task
变量作为更新数据传递,所以您不需要像在find.ref(id)
那样找到Task
对象的引用。 而且, play.db.ebean.Model
类(带一个参数)的update()
方法需要模型ID作为参数。
希望这有助于解决您的问题.. 🙂
确保您已在application.conf中注释掉Ebean配置
# Ebean configuration # ~~~~~ # You can declare as many Ebean servers as you want. # By convention, the default server is named `default` # ebean.default="models.*"