circumflex-orm ,I have connected the database ,and I can query record from the database,but when update and save ,it didn't work!

48 views
Skip to first unread message

litman

unread,
Sep 2, 2012, 11:29:49 PM9/2/12
to circumfl...@googlegroups.com
hi ,everyone
     I am new to play2 and circumflex,and know i encounted a problem which boring me much.
this is my class with circumflex

class User extends Record[Long, User] with IdentityGenerator[Long, User] {
  def this(username: String, password: String, status: Int, descn: String, id: Long) = {
    this()
    this.username := username.trim()
    this.password := password
    this.status := status
    this.descn := descn
    if (id == 0L) this.id.setNull else this.id := id
  }

//  def this(username: String, password: String) = {
//    this()
//    this.username := username.trim()
//    this.password := password
//  }

  def relation = User
  def PRIMARY_KEY = id

  val descn = "descn".VARCHAR(200)
  val username = "username".VARCHAR(50).NOT_NULL
  val id = "id".BIGINT.NOT_NULL.AUTO_INCREMENT
  val password = "password".VARCHAR(50).NOT_NULL
  val status = "status".INTEGER

}
and this is my object
object User extends User with Table[Long, User] {
  override def isAutoRefresh: Boolean = true
//  def apply(username: String, password: String):User =this(username,password)
  def apply(username: String, password: String, status: Int, descn: String, id: Long):User=new User(username, password, status, descn,0L)
  private val userDbObj = this AS "userDbObj"

  def critStand = userDbObj.criteria
  validation.notEmpty(_.username)

  def findUserByName(name: String): Seq[User] =
    (this AS "u").map(u => SELECT(u.*).FROM(u).WHERE(u.username EQ name).list)

  def findUser(): Seq[User] =
    (this AS "u").map(u => SELECT(u.*).FROM(u).WHERE(u.username IS_NOT_NULL).list)

  def findById(id: Long): Option[User] = {
    (this AS "u").map(u => SELECT(u.*)
      .FROM(u)
      .WHERE((u.id EQ id))
      .unique)

  }

  def update(u: User) = {
    println(u.username())
    println(u.UPDATE(u.id, u.username, u.descn, u.status, u.password))
  }

  def save(u: User): String = {

    println(u.username())
    println(u.save())
//    COMMIT()       
    u.refresh()
    ""
  }

}


when I excute save and update ,the value u.save() returns 1,same to update method,I wonder why it can't work with a COMMIT() which have annote it,and I find this is Google,I can't find a result at it
could someone can help me ?

Boris Okunskiy

unread,
Sep 3, 2012, 1:16:03 AM9/3/12
to circumfl...@googlegroups.com
Greetings litman,

We had lots of talks about transaction demarcation in Circumflex. Indeed, every database communication must occur in transaction. Circumflex defines special semantics for transaction called Context. So all you need to do is to wrap your database calls into `Context.executeInNew { ctx => … }` block. Then you don't have to call `COMMIT` -- the transaction will be committed automatically at the end of the context block.

As for the transaction demarcation with Play, make sure you read this topic: https://groups.google.com/forum/#!topic/circumflex-scala/ma2uCVgPx1Q

Hope it helps.

Best regards,
Boris Okunskiy

litman

unread,
Sep 3, 2012, 2:25:36 AM9/3/12
to circumfl...@googlegroups.com
thank you for answering me ,I have seen its before,and you said use `Context.executeInNew { ctx => … }`,or make a filter instead of  using commit(),is it necessary or  any other ways  in play2? I am glad to know even doesn't!

在 2012年9月3日星期一UTC+8下午1时16分06秒,Boris Okunskiy写道:

Boris Okunskiy

unread,
Sep 3, 2012, 2:36:08 AM9/3/12
to circumfl...@googlegroups.com
Yes, it is necessary (in fact, every ORM should work with clearly defined transaction boundaries).

I am not aware of Play2 request lifecycle. All you need to know is that the transaction should be initialized _before_ the request processing, and committed _afterwards_. Seems like a perfect work for filters, but I don't know whether Play2 supports filtering requests. I guess you'll have to ask this question on Play2 forums instead.

Best regards,
Boris Okunskiy

litman

unread,
Sep 3, 2012, 4:13:18 AM9/3/12
to circumfl...@googlegroups.com
Thanks very much!

在 2012年9月3日星期一UTC+8下午2时36分10秒,Boris Okunskiy写道:
Reply all
Reply to author
Forward
0 new messages