Deletions are marked like this. | Additions are marked like this. |
Line 61: | Line 61: |
=== レコードの挿入 == | === レコードの挿入 === |
Circumflex ORM
参考
とにかく動かしてみる
Circumflex ORMはCircumflexというウェブフレームワークの一部だが、ここでは単体で使用する。 DBはH2を用いた。
テーブルの定義とデータベース生成
Country, Cityはサンプルそのまま
import ru.circumflex._ import ru.circumflex.orm._ import ru.circumflex.core._ class Country extends Record[String, Country] { val code = "code".VARCHAR(2).NOT_NULL.DEFAULT("'ch'") val name = "name".TEXT.NOT_NULL def cities = inverseMany(City.country) def relation = Country def PRIMARY_KEY = code } object Country extends Country with Table[String, Country] class City extends Record[Long, City] with SequenceGenerator[Long, City] { val id = "id".BIGINT.NOT_NULL.AUTO_INCREMENT val name = "name".TEXT val country = "country_code".TEXT.NOT_NULL .REFERENCES(Country) .ON_DELETE(CASCADE) .ON_UPDATE(CASCADE) def relation = City def PRIMARY_KEY = id } object City extends City with Table[Long, City] object Creation { def main(args: Array[String]) { val cx = Circumflex cx("orm.connection.driver") = "org.h2.Driver" cx("orm.connection.url") = "jdbc:h2:sample" cx("orm.connection.username") = "sa" cx("orm.connection.password") = "" val unit = new DDLUnit(Country, City) unit.CREATE() } }
レコードの挿入
val country = new Country country.code := "jp" country.name := "japan" try { country.INSERT_!() COMMIT() } catch { case e:Exception => { e.printStackTrace(); } }