Search code examples
scalasqueryl

How do I use Squeryl's KeyedEntity?


The only information I could find relating to KeyedEntity was:

Objects that extend KeyedEntity[K] where K is a numeric type will have their id field assigned their newly created primary key value (the mechanism for generating keys is specific to each DatabaseAdaptor).

How does this apply when my keyed object is immutable? Then isn't it impossible for my object to have its id field assigned to a newly created primary key value?

I assume I am supposed to override the abstract id method inside KeyedEntity on my object in order to return the key of my object. But how does Squeryl find out which field to use as the key when it's writing to the database?

I'm using Squeryl 9.5 if it matters. I can't find much documentation on these issues.


Solution

  • In 0.95, Squeryl will look for a field id or in the case of CompositeKey, a function named id. So, either of these definitions would work:

    case class MyTable(val id:Long, ...) extends KeyedEntity[Long]
    
    case class MyTable(val field1:Long, val field2:String, ...) extends KeyedEntity[CompositeKey2[Long, String]] {
      def id = compositeKey(field1, field2)
    } 
    

    If you are using a single field, it looks for the db field named id, or you can use a @Column annotation to specify an alternate name in the db. In the case of a CompositeKey, it will look for the db fields corresponding to what the key is made up of (in the example above: field1 and field2). You can always use @Column to map to a different column name in the db.

    As was mentioned in the other answer you can alias most fields, however id doesn't work that way in 0.95 - see: ClassCastException when trying to insert with Squeryl. In 0.96 things change around a bit, as KeyedEntity is no longer required which you can find more info on here.

    If you are curious about how the internals of the insert method works, you can take a look at the source of Table on github.