ClassCastException: java.lang.Long cannot be cast to java.lang.Integer

691 views
Skip to first unread message

Yang Zhang

unread,
Aug 3, 2011, 3:33:21 PM8/3/11
to circumfl...@googlegroups.com
I'm seeing this runtime error:

[info] 12:24:51.110 [main] DEBUG ru.circumflex.orm - SELECT LASTVAL() AS this_1
[error] java.lang.ClassCastException: java.lang.Long cannot be cast to
java.lang.Integer
[error] at scala.runtime.BoxesRunTime.unboxToInt(Unknown Source)
[error] at scala.Tuple2._2$mcI$sp(Tuple2.scala:22)
[error] at com.pod.db.DbSetup$$anonfun$mock$1.apply$mcV$sp(db.scala:37)
[error] at com.pod.db.DbSetup$$anonfun$mock$1.apply(db.scala:34)
[error] at com.pod.db.DbSetup$$anonfun$mock$1.apply(db.scala:34)
[error] at
ru.circumflex.orm.package$$anonfun$using$1.apply(package.scala:41)
[error] at
ru.circumflex.orm.package$$anonfun$using$1.apply(package.scala:39)
[error] at ru.circumflex.core.Context$.executeInNew(context.scala:83)
...

This happens when I have a simple model:

class Account extends Record[Int, Account] with IdentityGenerator[Int,
Account] {
val id = "id".INTEGER.NOT_NULL.AUTO_INCREMENT
val name = "name".TEXT
def PRIMARY_KEY = id
def relation = Account
}

object Account extends Account with Table[Int, Account]

and run the following:

val a = new Account
a.name := "Yang"
a.save()
val aid = a.id() // failure here

Are you also seeing this?

--
Yang Zhang
http://yz.mit.edu/

Yang Zhang

unread,
Aug 3, 2011, 3:37:11 PM8/3/11
to circumfl...@googlegroups.com
Also...any ideas on how to work around this? Still trying to figure it out....

Boris Okunskiy

unread,
Aug 4, 2011, 2:41:14 AM8/4/11
to circumfl...@googlegroups.com
Hi Yang,

As you've probably already figured, neither IdentityGenerator nor the corresponding part of Dialect object contains explicit declaration of type `Long` expected from database query. What database are you using? Maybe you should override `identityLastIdQuery` in dialect to provide more specific database type? E.g. `new Select(expr[PK]("LASTVAL() :: INTEGER"))`

Best regards
Boris Okunskiy

Yang Zhang

unread,
Aug 4, 2011, 3:06:32 PM8/4/11
to circumfl...@googlegroups.com
On Wed, Aug 3, 2011 at 11:41 PM, Boris Okunskiy <inca...@whiteants.net> wrote:
> Hi Yang,
>
> As you've probably already figured, neither IdentityGenerator nor the corresponding part of Dialect object contains explicit declaration of type `Long` expected from database query. What database are you using? Maybe you should override `identityLastIdQuery` in dialect to provide more specific database type? E.g. `new Select(expr[PK]("LASTVAL() :: INTEGER"))`

I'm using Postgresql.

Boris Okunskiy

unread,
Aug 5, 2011, 12:16:42 AM8/5/11
to circumfl...@googlegroups.com
Okay, PostgreSQL's lastval() function always return BIGINT (http://www.postgresql.org/docs/8.4/interactive/functions-sequence.html), so you should use BIGINT instead of INTEGER. If you still need INTEGER auto-incremented data type and do realize its size limitation, then you should override the methods I've described above to introduce database-level casts.

Best regards,
Boris Okunskiy 

Yang Zhang

unread,
Aug 5, 2011, 3:02:57 AM8/5/11
to circumfl...@googlegroups.com
I have made this change. Thanks for the explanation.

This does seem to be more of a detail that I would expect the ORM to
mask, since it's safe to just make this cast knowing that the PK is an
Int, and since I would expect many users naturally use INTEGERs as
PKs. Maybe at least a mention in the documentation would be merited?
Just my 2c.

Boris Okunskiy

unread,
Aug 5, 2011, 10:21:14 AM8/5/11
to circumfl...@googlegroups.com
Well, Circumflex along with the site which hosts documentation are open-source, so you are totally welcome to contribute. We will appreciate every effort.

BTW, massive documentation update and Circumflex book is on our top priority list, but we simply can't afford this work right now.

Best regards,
Boris Okunskiy

Reply all
Reply to author
Forward
0 new messages