Reputation: 20090
I have an example DB in a scala object, but the tables are not created.
If I call the initialize and then I call the addEntries, I get a Table not existing exception by the JDBC layer. Where am I wrong?
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: _*)
}
}
Upvotes: 0
Views: 212
Reputation: 20090
I have found the problem: the h2 in memory driver does not store data among the session, unless:
jdbc:h2:mem:test;DB_CLOSE_DELAY=-1.
is specified
(See http://www.h2database.com/html/features.html)
Upvotes: 1