google-code-export / nutz

Automatically exported from code.google.com/p/nutz
Apache License 2.0
1 stars 1 forks source link

NutDao主键类型转换错误 #290

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
问题产生的条件:

 某PO的主键如下

@Column()
@Id(auto = false)
private long id;

发生问题的调用代码:

dao.delete(obj);

问题的详细说明:

主键Id为手动设置,值较大时会出现转换异常。

异常堆栈:

org.nutz.castor.FailToCastObjectException: Fail to cast from <java.lang.Long> 
to <java.lang.Integer> for {8568826056221878580} because:
NumberFormatException:For input string: "8568826056221878580"
    at org.nutz.castor.Castors.cast(Castors.java:239)
    at org.nutz.castor.Castors.castTo(Castors.java:293)
    at org.nutz.dao.impl.NutDao._deleteSelf(NutDao.java:383)
    at org.nutz.dao.impl.NutDao.delete(NutDao.java:412)
    at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)

问题跟踪:

问题原因在NutDao#int _deleteSelf(Entity<?> entity, Object obj) 方法

其中:
if (idnf.isId()) {
    int id = Castors.me().castTo(idnf.getValue(obj), Integer.class);
    return delete(obj.getClass(), id);
}
指定了转换类型为Integer.class
当Long类型且值较大时会出现转换异常

Original issue reported on code.google.com by yonl...@163.com on 18 Sep 2010 at 7:58

GoogleCodeExporter commented 9 years ago
调试后发现:
将该行代码
int id = Castors.me().castTo(idnf.getValue(obj), Integer.class);
改为
long id = Castors.me().castTo(idnf.getValue(obj), Long.class);
可解决该问题

Original comment by yonl...@163.com on 18 Sep 2010 at 8:12

GoogleCodeExporter commented 9 years ago
fixed in r1540

Original comment by zozoh...@gmail.com on 19 Sep 2010 at 5:07