【问题标题】:Querying database using Scala console in Java Play在 Java Play 中使用 Scala 控制台查询数据库
【发布时间】:2015-10-10 16:27:28
【问题描述】:

我是 Play 新手,我正在尝试测试它的 shell(和 Scala 控制台)以从数据库中查询值。 我正在使用 Play 2.4.3 和 H2(数据库)

从这里:https://playlatam.wordpress.com/2012/04/01/play-framework-2-quicktip-interactively-play-with-your-application-from-the-scala-console/ 在这里:https://github.com/playframework/playframework/issues/4593 到目前为止,我已经尝试过但没有成功:

进入控制台

./activator shell

然后

console

进入 Scala 控制台后,我会这样做:

import play.api._
val env = Environment(new java.io.File("."), this.getClass.getClassLoader, Mode.Dev)
val context = ApplicationLoader.createContext(env)
val loader = ApplicationLoader(context)
val app = loader.load(context)
Play.start(app)
import Play.current

我有名为 Question 和 Choice 的模型,因此我将它们导入:

import models._

(也试过,models.Question等组合)。然后,当我尝试通过执行(和类似的变体)来查询它时:

val questions = Question.all()

我得到了错误

<console>:8: error: value all is not a member of object models.Question
   val questions = Question.all()
                            ^

作为参考,您可以在下面找到我的问题模型:

package models;

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

import com.avaje.ebean.Model;
import play.data.format.*;
import play.data.validation.*;

@Entity
public class Question extends Model {

    @Id
    public Integer id;

    @Constraints.Required
    public String question_text;

    @Constraints.Required
    @Formats.DateTime(pattern="dd/MM/yyyy")
    public Date pub_date = new Date();

    @OneToMany(mappedBy = "question")
    public List<Choice> choices = new ArrayList<>();
}

理论上,我已经使用 activator run 命令(它为我提供了更新数据库的脚本)将此方案添加到数据库中。我正在使用基于文件的 H2。

另外,我尝试添加一个新问题

val questions = models.Question(1, "is this a question", format.parse("21-03-2011"))

格式是:

val format = new java.text.SimpleDateFormat("dd-MM-yyyy")

我得到了错误:

   <console>:8: error: object models.Question is not a value
       val questions = models.Question(1, "is this a question", format.parse("21-03-2011"))

问题是: 那么,如何使用 Scala 控制台添加值并查询它们呢?

编辑: 以防万一,您可以按照接受的答案添加项目,然后可以使用 .save() 方法将它们保存到数据库中。

【问题讨论】:

    标签: java scala playframework


    【解决方案1】:

    我认为您正在混合使用 Java 和 Scala 类,以及使用两者的方式。

    val questions = Question.all()

    这不起作用,因为Question 类没有任何名为all 的静态方法(该方法属于Mode.Finder)。所以你必须在你的类中创建一个查找器,并可能创建这些方法:

    public class Question extends Model {
        // (...)
        public static final Finder<Integer, Question> find =
                                                new Finder<>(Question.class);
    
        public static List<Question> all() {
            return find.all();  
        }
    
    }
    
    val questions = Question.all
    // or
    val questions = Question.find.all
    

    您可以在Model.Finder documentation 中找到更多信息。

    val questions = models.Question(1, "这是一个问题吗", format.parse("21-03-2011"))

    只有在Question 是 Scala 案例类时,这种语法才能开箱即用。 同样,您将不得不使用new,如果您不想在之后指定每个参数,请使用您要使用的参数实现一个新的构造函数:

    val question = new Question
    question.id = 1
    question.question_text = "is this a question"
    // (...)
    

    【讨论】:

    • 我可以使用该方法创建一个问题(scala> question res2: models.Question = models.Question@299f54ed),但我仍然无法查询数据库。还有其他方法可以实现吗?错误仍然是:scala> val questions = (new Question).all :14: error: value all is not a member of models.Question val questions = (new Question).all
    • @zom-pro 对不起,你是对的。您需要实现一个查找器。我已经用这些信息编辑了答案。
    • 我现在可以查询数据库,但我无法向其中插入新值。我有一种感觉,我需要一个“插入”属性(因为我需要一个 find 来查询)。你知道我在哪里可以找到它的正确语法吗?
    • 想通了,可以做question.save()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    相关资源
    最近更新 更多