전에 지수형에게 배운 Slick 를 기반으로 플레이에서 ORM을 구현하려 하다가 최근 Slick 2.0으로 올라가면서 좀 이해하기 힘든 부분이 많았다는 것이다. 레퍼런스도 부족해서 인터넷을 찾아보면 그나마 아웃사이더 님 글밖에 없고.. 특히 두개 이상의 뷰를 통합하려 하다보면 모델도 두개를 이어줘야 하는데 그부분을 DB에서 처리하고 싶은데 도통 방법을 모르겠더라. 결국 DB의 ORM 처리는 그나마 가장 익숙했던 Anorm 으로 가기로 했다.
|
User.scala case class User(seq:Pk[Long], name:String, email:String, pic_url:String, win:Int, lose:Int, total:Int, token:String,joinDate:String, current_location:Int, location_x:Int, location_y:Int, seq_current_room:Int) … Game.scala case class Game(seq:Pk[Long], seq_room:Int, turn:Int, score_owner:Int, score_enemy:Int) … Room.scala case class Room(seq:Pk[Long], id_owner:String, id_player:Option[String], title:String, time:Int, privacy:Int, game_status:Int,gameinfo:Game, owner:User, player:User=null, watcher:Seq[User]=null) … val withUser = { get[Pk[Long]](“roominfo.seq”) ~ get[String](“roominfo.id_owner”) ~ get[Option[String]](“roominfo.id_player”) ~ get[String](“roominfo.title”) ~ get[Int](“roominfo.time”) ~ get[Int](“roominfo.privacy”) ~ get[Int](“roominfo.game_status”) ~ Games.simple ~ Users.simple map { case seq ~ id_owner ~ id_player ~ title ~ time ~ privacy ~ game_status ~ gameinfo ~ owner => Room(seq, id_owner, id_player, title, time, privacy, game_status, gameinfo, owner) } } def getRooms:Seq[Room] = DB.withConnection { println(“=== Rooms – getRooms()”) implicit connection => SQL( “”” select A.*,B.*,C.* from roominfo A, gameinfo B, userinfo C where A.game_status != 0 AND A.seq = B.seq_room AND A.id_owner = C.token “”” ).as(Rooms.withUser *) } |
위에서처럼 DB에서 세개의 테이블을 참조해서 하나의 객체를 만들고자 할때, 저런식으로 처리하면 꽤 편리하게 Room 객체를 Game과 User 클래스를 가지고 있도록 만들 수 있다.



