ORM supporting immutable classes

前端 未结 6 801
暖寄归人
暖寄归人 2021-02-03 22:15

Which ORM supports a domain model of immutable types?

I would like to write classes like the following (or the Scala equivalent):

class          


        
6条回答
  •  遥遥无期
    2021-02-03 22:48

    SORM is a new Scala ORM which does exactly what you want. The code below will explain it better than any words:

    // Declare a model:
    case class Artist ( name : String, genres : Set[Genre] )
    case class Genre ( name : String ) 
    
    // Initialize SORM, automatically generating schema:
    import sorm._
    object Db extends Instance (
      entities = Set() + Entity[Artist]() + Entity[Genre](),
      url = "jdbc:h2:mem:test"
    )
    
    // Store values in the db:
    val metal = Db.save( Genre("Metal") )
    val rock = Db.save( Genre("Rock") )
    Db.save( Artist("Metallica", Set() + metal + rock) )
    Db.save( Artist("Dire Straits", Set() + rock) )
    
    // Retrieve values from the db:
    val metallica = Db.query[Artist].whereEqual("name", "Metallica").fetchOne() // Option[Artist]
    val rockArtists = Db.query[Artist].whereEqual("genres.name", "Rock").fetch() // Stream[Artist]
    

提交回复
热议问题