Created
October 9, 2014 15:56
-
-
Save mocyuto/d7ed85b27834688607e1 to your computer and use it in GitHub Desktop.
updateでcase classの値が埋まっているところのみ更新
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Coffees(id: Int, name: String, supId:Option[Int], price: Option[Double]) | |
class CoffeesTable(tag: Tag) extends Table[(String, Int, Double, Int, Int)](tag, "coffees") { | |
def id = column[Int]("COF_NAME", O.PrimaryKey) | |
def name = column[String]("name") | |
def supID = column[Int]("SUP_ID") | |
def price = column[Double]("PRICE") | |
def * = (id, name, supID, price) <> (Coffees.tupled, Coffees.unapply) | |
} | |
val query = TableQuery[Coffees] | |
def update (coffee: Coffees) = { | |
Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver") withSession { | |
implicit session => | |
// select | |
query.filter(_.id === id).firstOption | |
// javaのリフレクションでcase classの値を書き換える | |
coffee.getClass.getDeclaredFields.map { f => | |
f.setAccessible(true) | |
f.get(entity) match { | |
case Some(s) => f.set(source, Some(s)) | |
case _ => | |
} | |
f.setAccessible(false) | |
} | |
query.filter(id===coffee.id).update(coffee) | |
} | |
} | |
def main(args: Array[String]) { | |
update(coffee(1,"starbacks",None,None)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment