Question

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.

Was it helpful?

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.

OTHER TIPS

Then isn't it impossible for my object to have its id field assigned to a newly created primary key value?

It's impossible using normal standard ways, yes, but that's not what's going on. We're not modifying objects -- we're creating new ones. For example, the method .insert(myEntry) returns you the object that has been inserted when you called that method, not your original myEntry.

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.

No, you don't have to do it -- you can leave it blank. And if you name the table field different from "id" -- don't forget to include a function also, like def id = myIdField.

But how does Squeryl find out which field to use as the key when it's writing to the database?

It seaches a method with the exact name -- "id". Or, a manual function like I wrote earlier.

Answering the topic title -- one use case of KeyedEntity is, for example, to call methods like myEntities.lookup(30L). Or, to be able to call "updateOrInsert" method. To auto-define "Primary Key" when creating a new database fro scratch. And others...

I can't find much documentation on these issues.

Here's the lookup method, for example: http://squeryl.org/selects.html Actually, everything I wrote now I learned from the site some time ago.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top