ClassCastException when trying to insert with Squeryl

本小妞迷上赌 提交于 2019-12-24 20:36:54

问题


This may be due to my misunderstanding of how Squeryl works. My entity is defined as:

case class Wallet(userid: Int, amount: Long) 
    extends KeyedEntity[Int] with Optimistic {  
  def id = userid
}  

My table variable is defined as:

val walletTable = table[Wallet]("wallets")
on(walletTable) {
 w =>
   declare {
     w.userid is (primaryKey)
   }
}

Then I'm just calling a method to try to add money to the wallet:

val requestedWallet = wallet.copy(amount = wallet.amount + amount)
try {
  inTransaction {
    walletTable.update(requestedWallet)
  }

On the line where I call update, an exception is getting thrown: [ClassCastException: java.lang.Integer cannot be cast to org.squeryl.dsl.CompositeKey]

I'm not using composite keys at all, so this is very confusing. Does it have to do with the fact that my id field is not called "id", but instead "userid"?


回答1:


I get the same behavior when I try what you are doing. It seems that for some reason id can't be an alias unless it is a composite key (at least in 0.9.5). You can work around that and get the same result with something like this:

case class Wallet(@Column("userid") id: Int, amount: Long)
  extends KeyedEntity[Int] with Optimistic  {  

  def userid = id

}  

The column annotation will look to the database for the userid field, and id will be a val instead. You can then alias userid for consistency.



来源:https://stackoverflow.com/questions/13939922/classcastexception-when-trying-to-insert-with-squeryl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!