Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7928105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:38:56+00:00 2026-06-03T19:38:56+00:00

I am looking for best practices regarding models and ways to persist objects in

  • 0

I am looking for best practices regarding models and ways to persist objects in database with play 2.0. I have studied the Play and typesafe samples for play 2.0 using scala.

What I understand is :

  • The model is defined in a case class
  • All the insert/update/delete/select are defined in the companion object of this case class

So if I need to update my Car object to define a new owner i will have to do:

val updatedCar = myCar.copy(owner=newOwner)
Car.update(updatedCar)
// or
Car.updateOwner(myCar.id.get, newOwner)

I am wondering why the update or delete statements are not in the case class itself:

case class Car(id: Pk[Long] = NotAssigned, owner: String) {
    def updateOwner(newOwner: String) {
        DB.withConnection { implicit connection =>
            SQL(
                """
                update car
                set owner = {newOwner}
                where id = {id}
                """
            ).on(
                'id -> id,
                'newOwner -> newOwner
            ).executeUpdate()
        }
        copy(owner = newOwner)
    }
}

Doing so would permit to do:

val updatedCar = myCar.updateOwner(newOwner)

Which is what I used to do with Play 1.X using Java and JPA.
Maybe the reason is obvious and due to my small knowledge of Scala.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:38:57+00:00Added an answer on June 3, 2026 at 7:38 pm

    I think part of the reason is the favoring of immutability in functional languages like Scala.

    In your example, you modify ‘this.owner’. What’s your equivalent operation look like for a delete, and what happens to “this”?

    With a companion object, it seems a bit more clear that the passed object (or ID) is not modified, and the returned object or ID is the relevant result of the operation.

    Then also, I think another part of the issue is that your example requires an instance first. When you delete an Object, what if you just want to delete by Id you got off a form, and don’t want to first build a whole instance of the object you intend to delete?

    I’ve been playing with play2.0 with mongo, and my companion objects look like:

    object MyObject extends SalatDAO[MyObject,ObjectId] (collection = getCollection(“objectcollection”)) {
    }

    These companion objects inherit CRUD like operations from SalatDAO (MyObject.save(), MyObject.find(), etc). I’m not entirely clear on how it is implemented internally, but it works nicely.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for insight into best practices regarding database scripting for modifications that go
I am using the NHibernate.Search assembly and am looking for best practices around using
I am looking for ways / best practices on testing methods defined in an
I am looking for any best practices or guidelines regarding animation in WPF. Especially
I'm looking for best practices regarding abstracting my RIA domain context away from my
I am looking for the best practices regarding the usage of the Enterprise Library's
I have a question about best practices regarding how one should approach storing complex
I'm looking for best practices here. Sorry. I know it's subjective, but there are
Web.Config? Website properties? I'm looking for suggestions/best practices with reasons you recommend them.
Looking for some resources on SOA / BPEL best practices. Specially BPEL.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.