zom-pro
zom-pro

Reputation: 1649

Querying database using Scala console in Java Play

I'm new to Play and I'm trying to test its shell (and Scala console) to query values from the database. I'm using Play 2.4.3 and H2 (database)

From here: https://playlatam.wordpress.com/2012/04/01/play-framework-2-quicktip-interactively-play-with-your-application-from-the-scala-console/ and here: https://github.com/playframework/playframework/issues/4593 So far I've tried without success:

To get into the console

./activator shell

And then

console

Once I'm in the Scala console, I do:

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

I've got models called Question and Choice, so I import them doing:

import models._

(Also tried, models.Question and other combinations). Then when I try to query it by doing (and similar variants):

val questions = Question.all()

I get the error

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

As a reference, you can find my Question model below:

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<>();
}

I've, in theory added, this scheme into the database using the activator run command (which offered me the script to update the DB). I'm using H2, file-based.

Also, I tried adding a new question

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

Where format is:

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

and I get the error:

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

The question is: So, how do I add values and query them back using the Scala console?

EDITED: Just in case, you can add items as shown in the accepted answer and you can save them into the db using .save() method.

Upvotes: 1

Views: 242

Answers (1)

Salem
Salem

Reputation: 12986

I think you are mixing Java and Scala classes, and the way to use both.

val questions = Question.all()

This can't work because Question class does not have any static method called all (that method belongs to Mode.Finder). So you will have to create a finder in your class and maybe create those methods:

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

You can find more info in the Model.Finder documentation.

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

This syntax would only work out of the box if Question was a Scala case class. Again you will have to use new, and if you don't want to specify each parameter after, implement a new constructor with the parameters you want to use:

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

Upvotes: 1

Related Questions