更新任务模型 - RuntimeException:DataSource用户是否为空? [英] Update the Task model - RuntimeException: DataSource user is null?
问题描述
我今天开始学习Play框架,它非常好且易于学习。
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)
然后我将以下代码添加到 index.scala.html
file
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 />
}
然后我将Application.java类修改为
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());
}
}
最后在Task.java中我添加了这段代码
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用户为空?]
[RuntimeException: DataSource user is null?]
不用说我注释掉了
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
ebean.default="models.*"
在application.conf中,因为我已经能够保存和删除数据;但我无法更新数据库中的数据,为什么会发生这种情况,之前有人试过这个,我该如何解决这个错误?
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?
推荐答案
任务
模型上的更新(长期ID,任务任务)
方法应如下所示:
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
}
因为你传递了任务
变量作为更新数据,您不需要像任务
对象的引用> find.ref(ID)。而且, update()
方法 play.db.ebean.Model
class(带一个参数)需要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.. :)
这篇关于更新任务模型 - RuntimeException:DataSource用户是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!