我在 scala 对象中有一个示例数据库,但未创建表。
如果我调用初始化然后调用 addEntries,我会得到 JDBC 层的 Table notexisting 异常。我哪里错了?
object ExampleCompanyH2TestDb {
val Companies = new Table[(Int, Int, String, String, String, Double,Double)]("COMPANIES") {
def id = column[Int]("COMPANY_ID", O.PrimaryKey)
// This is the primary key column
def id2 = column[Int]("COMPANY_ID2")
def name = column[String]("COMPANY_NAME")
def country = column[String]("COUNTRY")
def city = column[String]("CITY")
def latitude = column[Double]("LATITUDE")
def longitude = column[Double]("LONGITUDE")
// Every table needs a * projection with the same type as the
table's type parameter
def * = id ~ id2 ~ name ~ country ~ city ~ latitude ~ longitude
}
// Connect to the database and execute the following block within a session
val db = Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver")
def initialize = {
db withSession {
// The session is never named explicitly. It is bound to the current
// thread as the threadLocalSession that we imported
(Companies.ddl).create
}
}
// Create the tables, including primary and foreign keys
def addEntries(entries: Iterable[ExampleCompany]) {
val asTuples = entries.map {
ExampleCompany.unapply
}.collect {
case Some(entry) => entry
}.toSeq
db withSession {
Companies.insertAll(asTuples: _*)
}
}