当前位置: 首页 > 知识库问答 >
问题:

带H2的Scala Slikk 0.11.2计数

隆长卿
2023-03-14

我试图用https://github.com/slick/slick-examples/blob/master/src/main/scala/scala/slick/examples/lifted/firstexample.scala:

// Definition of the SUPPLIERS table
object Suppliers extends Table[(Int, String, String, String, String, String)]("SUPPLIERS")        {
  def id = column[Int]("SUP_ID", O.PrimaryKey) // This is the primary key column
  def name = column[String]("SUP_NAME")
  def street = column[String]("STREET")
  def city = column[String]("CITY")
  def state = column[String]("STATE")
  def zip = column[String]("ZIP")
  // Every table needs a * projection with the same type as the table's type parameter
  def * = id ~ name ~ street ~ city ~ state ~ zip
}

// Definition of the COFFEES table
object Coffees extends Table[(String, Int, Double, Int, Int)]("COFFEES") {
  def name = column[String]("COF_NAME", O.PrimaryKey)
  def supID = column[Int]("SUP_ID")
  def price = column[Double]("PRICE")
  def sales = column[Int]("SALES")
  def total = column[Int]("TOTAL")
  def * = name ~ supID ~ price ~ sales ~ total
  // A reified foreign key relation that can be navigated to create a join
  def supplier = foreignKey("SUP_FK", supID, Suppliers)(_.id)
}

// Connect to the database and execute the following block within a session
Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver") withSession {

  (Suppliers.ddl ++ Coffees.ddl).create

  // Insert some suppliers
  Suppliers.insert(101, "Acme, Inc.",      "99 Market Street", "Groundsville", "CA", "95199")
  Suppliers.insert( 49, "Superior Coffee", "1 Party Place",    "Mendocino",    "CA", "95460")
  Suppliers.insert(150, "The High Ground", "100 Coffee Lane",  "Meadows",      "CA", "93966")

  // Insert some coffees (using JDBC's batch insert feature, if supported by the DB)
  Coffees.insertAll(
    ("Colombian",         101, 7.99, 0, 0),
    ("French_Roast",       49, 8.99, 0, 0),
    ("Espresso",          150, 9.99, 0, 0),
    ("Colombian_Decaf",   101, 8.99, 0, 0),
    ("French_Roast_Decaf", 49, 9.99, 0, 0)
  )

  /**** This part works fine */
  val query = for (c <- Coffees) yield c
  query foreach println
  /*********/

  /******This one fails */
  val query1 = for (c <- Coffees) yield c.length
  println("query1 = " + query1.list.head)

    Column "X4.COF_NAME" not found; SQL statement:
    CREATE FORCE VIEW PUBLIC._1 AS
    SELECT
        X4.COF_NAME,
        X4.SUP_ID,
        X4.PRICE,
        X4.SALES,
        X4.TOTAL
    FROM SYSTEM_RANGE(1, 1) [42122-168]
    org.h2.jdbc.JdbcSQLException: Column "X4.COF_NAME" not found; SQL statement:
    CREATE FORCE VIEW PUBLIC._1 AS
    SELECT
        X4.COF_NAME,
        X4.SUP_ID,
        X4.PRICE,
        X4.SALES,
        X4.TOTAL
    FROM SYSTEM_RANGE(1, 1) [42122-168]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
        at org.h2.message.DbException.get(DbException.java:169)
        at org.h2.message.DbException.get(DbException.java:146)
        at org.h2.expression.ExpressionColumn.optimize(ExpressionColumn.java:138)
        at org.h2.command.dml.Select.prepare(Select.java:799)
        at org.h2.command.Parser.prepare(Parser.java:202)
        at org.h2.engine.Session.prepare(Session.java:388)
        at org.h2.engine.Session.prepare(Session.java:375)
        at org.h2.table.TableView.compileViewQuery(TableView.java:99)
        at org.h2.table.TableView.initColumnsAndTables(TableView.java:144)
        at org.h2.table.TableView.init(TableView.java:95)
        at org.h2.table.TableView.(TableView.java:61)
        at org.h2.table.TableView.createTempView(TableView.java:420)
        at org.h2.command.Parser.readTableFilter(Parser.java:1037)
        at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1690)
        at org.h2.command.Parser.parseSelectSimple(Parser.java:1797)
        at org.h2.command.Parser.parseSelectSub(Parser.java:1684)
        at org.h2.command.Parser.parseSelectUnion(Parser.java:1527)
        at org.h2.command.Parser.readTableFilter(Parser.java:1026)
        at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1690)
        at org.h2.command.Parser.parseSelectSimple(Parser.java:1797)
        at org.h2.command.Parser.parseSelectSub(Parser.java:1684)
        at org.h2.command.Parser.parseSelectUnion(Parser.java:1527)
        at org.h2.command.Parser.parseSelect(Parser.java:1515)
        at org.h2.command.Parser.parsePrepared(Parser.java:405)
        at org.h2.command.Parser.parse(Parser.java:279)
        at org.h2.command.Parser.parse(Parser.java:251)
        at org.h2.command.Parser.prepareCommand(Parser.java:217)
        at org.h2.engine.Session.prepareLocal(Session.java:415)
        at org.h2.engine.Session.prepareCommand(Session.java:364)
        at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1109)
        at org.h2.jdbc.JdbcPreparedStatement.(JdbcPreparedStatement.java:74)
        at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:626)
        at scala.slick.session.Session$class.prepareStatement(Session.scala:29)
        at scala.slick.session.BaseSession.prepareStatement(Session.scala:201)
        at scala.slick.jdbc.StatementInvoker.results(StatementInvoker.scala:29)
        at scala.slick.jdbc.StatementInvoker.elementsTo(StatementInvoker.scala:17)
        at scala.slick.jdbc.Invoker$class.foreach(Invoker.scala:90)
        at scala.slick.jdbc.StatementInvoker.foreach(StatementInvoker.scala:10)
        at scala.slick.jdbc.Invoker$class.build(Invoker.scala:66)
        at scala.slick.jdbc.StatementInvoker.build(StatementInvoker.scala:10)
        at scala.slick.jdbc.Invoker$class.list(Invoker.scala:56)
        at scala.slick.jdbc.StatementInvoker.list(StatementInvoker.scala:10)
        at scala.slick.jdbc.UnitInvoker$class.list(Invoker.scala:150)
        at scala.slick.driver.BasicInvokerComponent$QueryInvoker.list(BasicInvokerComponent.scala:19)

如果使用

println("query1 = " + query1.firstOption.getOrElse(0))

还有...

共有1个答案

伯英武
2023-03-14

你试过以下方法吗?

val query1 = for (c <- Coffees) yield c.name.count
println("query1 = " + query1.firstOption.getOrElse(0))

它背后的配方是c.primary-key.count;在本例中,namecoffees的主键。

 类似资料:
  • 我使用gradle idea运行project,以更新依赖项,然后使用gradlew build和gradlew run。没有错误,但当我转到localhost:8080/h2-console时,我得到的只是连接错误。我也试过用gradle Build。为什么会出现这种情况?我也使用IntelIJ,我有红色的build和gradle文件夹,为什么?我使用的是这个指南:https://www.bae

  • 我医生看起来像 我想拥有超过 100个文档在50到100个之间少于100个文档我尝试使用不同的聚合,但我不知道如何在另一个聚合的计数上进行范围聚合 谢谢你的帮助,

  • 我正在尝试在spring boot应用程序启动时设置H2数据库。我已在Application.Properties中配置了数据库: 我在上获得一个。如何注入?我看到的所有示例都要求手动创建datasource,然后创建JdbcTemplate。但是,本例中的datasource似乎是基于application.properties值创建的。 这是设置数据库的正确方法吗(即在启动SpringAppl

  • H2

    H2是一个短小精干的嵌入式数据库引擎,主要的特性包括: 免费、开源、快速 嵌入式的数据库服务器,支持集群 提供JDBC、ODBC访问接口,提供基于浏览器的控制台管理程序 Java编写,可使用GCJ和IKVM.NET编译 短小精干的软件,1M左右。 几个嵌入式数据库的对比:   H2 Derby HSQLDB MySQL PostgreSQL Pure Java Yes Yes Yes No No

  • 我目前正在从事一个项目,在该项目中,我给出了一个实体,该实体具有一个集合,即订单,因此客户和订单之间存在一种单一关系。假设我想选择具有相应数量的订单实体(在SQL中称为计数)的客户集合的子集:Spring Data JPA提供的查询方法是否已经有了默认实现,以避免使用JPQL编写自定义查询,该查询在一个SQL查询中而不是在n 1个查询中获取客户订单的数量其中n是客户数量?或者可以在某种瞬态字段(例