val db = Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver")
db withSession {
object User extends Table[(Int, String)]("USERS") {
def id = column[Int]("ID", O.AutoInc, O.PrimaryKey)
def name = column[String]("NAME")
def * = id ~ name
}
val scopeIdentity =
SimpleScalarFunction.nullary[Long]("scope_identity")
User.ddl.create
User.name.insert("foo")
println("Inserted id "+Query(scopeIdentity).first)
User.name.insert("bar")
println("Inserted id "+Query(scopeIdentity).first)
}
H2's SCOPE_IDENTITY() function returns the last inserted identity value
for the current session (see
http://www.h2database.com/html/functions.html#scope_identity)
In practice, you'll probably want to cache the Invoker for scopeIdentity
or use a SimpleStatement, but this is the most general solution.
Creating a SimpleScalarFunction allows you to use SCOPE_IDENTITY() in
typed queries like you would use any built-in function.
-sz
--
Assuming that this was
opened by you and you're indeed using H2, this should work:val db = Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver")
db withSession {object User extends Table[(Int, String)]("USERS") {
def id = column[Int]("ID", O.AutoInc, O.PrimaryKey)
def name = column[String]("NAME")
def * = id ~ name
}val scopeIdentity =
SimpleScalarFunction.nullary[Long]("scope_identity")User.ddl.create
User.name.insert("foo")
println("Inserted id "+Query(scopeIdentity).first)
User.name.insert("bar")
println("Inserted id "+Query(scopeIdentity).first)
}H2's SCOPE_IDENTITY() function returns the last inserted identity value
for the current session (see
http://www.h2database.com/html/functions.html#scope_identity)In practice, you'll probably want to cache the Invoker for scopeIdentity
or use a SimpleStatement, but this is the most general solution.
Creating a SimpleScalarFunction allows you to use SCOPE_IDENTITY() in
typed queries like you would use any built-in function.