尝试使用Squeryl插入时发生ClassCastException [英] ClassCastException when trying to insert with Squeryl

查看:77
本文介绍了尝试使用Squeryl插入时发生ClassCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是由于我对Squeryl的工作方式有误解.我的实体定义为:

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
}  

我的表变量定义为:

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)
  }

在我称为update的那一行,抛出了一个异常: [ClassCastException:无法将java.lang.Integer强制转换为org.squeryl.dsl.CompositeKey]

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

我根本没有使用复合键,因此这非常令人困惑.我的id字段不是"id",而是"userid"吗?

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"?

推荐答案

当我尝试执行操作时,会得到相同的行为.似乎由于某些原因,id不能是别名,除非它是复合键(至少在0.9.5中).您可以解决该问题,并通过以下方式获得相同的结果:

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

}  

列注释将在数据库中查找userid字段,而id将是val.然后,您可以为userid加上别名以保持一致性.

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.

这篇关于尝试使用Squeryl插入时发生ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆