Equality issue when extending KeyedEntity[T]

35 views
Skip to first unread message

Jonathan Ferguson

unread,
Jan 21, 2015, 10:56:58 PM1/21/15
to squ...@googlegroups.com
Hello, 

I am having an issue with case classes which extend KeyedEntity[T].  When not persisted they are using instance equality which causes our tests to fail, unless we manually compare all fields.  We tried removing the KeyedEntity[T] trait, but we are dependent on it for auto increment to work. 

I am hoping I'm thinking about this incorrectly and there is a solution.  I've included a simple example to outline the issue.  The only constraint not included in the example is our dependency on PostgresSQL.


Thanks

Jono


Maxime Lévesque

unread,
Jan 22, 2015, 9:49:22 AM1/22/15
to Squeryl

Hi Jono ! 

You can implement a KeyedEntityDef and have it implicitely available in the scope and you'll get an auto incremented PK without the overridden equals, ex.:

  implicit object personKED extends KeyedEntityDef[Student,Int] {
    def getId(a:Student) = a.id
    def isPersisted(a:Student) = a.id > 0
    def idPropertyName = "id"
  }


The KeyedEntity is just a particular case :


The default behavior is that objects with KeyedEntityDef are auto incremented by default if the PK is numerical, but can be turned off.

Cheers !


--
You received this message because you are subscribed to the Google Groups "Squeryl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to squeryl+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jonathan Ferguson

unread,
Jan 22, 2015, 6:03:27 PM1/22/15
to squ...@googlegroups.com
Hey Max, 

Thank you that was super simple.  

For those that are interested, I've updated the example repository showing the solution.

Cheers 
Jono
Reply all
Reply to author
Forward
0 new messages