lishunli / nutz

Automatically exported from code.google.com/p/nutz
0 stars 0 forks source link

Dao: 执行 Trans.exec for informix 出现 Castors 错误 #126

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
by 清风徐来

问题产生的条件:
LuServiceAtom  :
public class LuServiceAtom implements Atom{
    private Dao dao = null;

    private IcpBASJ icpBASJ = null;

    public LuServiceAtom(Dao dao, IcpBASJ icpBASJ){
        this.dao = dao;
        this.icpBASJ = icpBASJ;
    }

    /**
     * 覆写线程的run方法
     */
    public void run() {
        Jricprecordip jricprecordip = new Jricprecordip();
        jricprecordip.setId(365);
        System.out.println(jricprecordip.getId());
        jricprecordip.setEndip("192.168.1.10");
        jricprecordip.setEndiplong(192);
        jricprecordip.setIpid(123);
        jricprecordip.setIsprecordid(123);
        jricprecordip.setJricprecordid(123);
        jricprecordip.setStartip("192.168.1.3");
        jricprecordip.setStartiplong(192);
        dao.insert(jricprecordip);
    }
}

Jricprecordip :就是一个基本的VO类  

发生问题的调用代码:
public static void main(String[] args) {

        /*
         * 获取DAO
         */
        Dao dao = null;
        try {
            dao = RecordDBUtil.getNutDao();
        } catch (Exception e) {
            e.printStackTrace();
        }

        /*
         * 实例化一个同步线程
         */
        Atom atom = new LuServiceAtom(dao, null);

        /*
         * 将同步线程放入事务中处理
         */
        try {
        Trans.exec(atom);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

异常堆栈:
java.lang.RuntimeException: Fail to set value for object 
[zzy.record.vo.jdbc.Jricprecordip]->[id], because: 'Can not find castor for 
'java.lang.String'=>'int' because:
Fail to find matched castor'
    at org.nutz.lang.Lang.makeThrow(Lang.java:65)
    at org.nutz.dao.entity.EntityField.setValue(EntityField.java:221)
    at 
org.nutz.dao.entity.next.FieldQueryImpl.update(FieldQueryImpl.java:88)
    at org.nutz.dao.impl.NutDao._insertSelf(NutDao.java:608)
    at org.nutz.dao.impl.NutDao.access$4(NutDao.java:595)
    at org.nutz.dao.impl.NutDao$11.invoke(NutDao.java:630)
    at org.nutz.lang.Lang.each(Lang.java:959)
    at org.nutz.dao.impl.NutDao.insert(NutDao.java:628)
    at zzy.record.service.LuServiceAtom.run(LuServiceAtom.java:71)
    at org.nutz.trans.Trans.exec(Trans.java:121)
    at org.nutz.trans.Trans.exec(Trans.java:79)
    at zzy.record.service.LuService.main(LuService.java:79)

相关日志:

Original issue reported on code.google.com by zozoh...@gmail.com on 10 Mar 2010 at 4:06

GoogleCodeExporter commented 9 years ago
首先, 通过这个问题发现:  EntityField.setValue 
函数有逻辑错误

try {
            if (null != value) {
                if (field.getType() != value)
                    value = Castors.me().castTo(value, 
field.getType());
            }
...

条件 field.getType() 与 value 
是永远不等的,所有这个判断没有意义,在 r916 
中,我做了修改,将等式
改为 if(field.getType() !=value.getClass())

Original comment by zozoh...@gmail.com on 10 Mar 2010 at 4:09

GoogleCodeExporter commented 9 years ago
通过异常堆栈,看起来是在做转换的时候*没有找到转换器*,
这个似乎不太可能,我测试了:
{{{
int num = Castors.me().castTo("23", int.class);
System.out.println(num);
}}}
看起来输出正常

希望能贴出 Jricprecordip  全部的代码,即使是一个简单的 
VO,这样我在本地模拟一下,看看能不能重现这个
问题

Original comment by zozoh...@gmail.com on 10 Mar 2010 at 4:20

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Jricprecordip  :
@Table("jricprecordip")
public class Jricprecordip implements Serializable {
    /**
     * This attribute maps to the column id in the jricprecordip table.
     */
    @Id
    @Column
    protected int id;

    /**
     * This attribute maps to the column jricprecordid in the jricprecordip
     * table.
     */
    @Column
    protected int jricprecordid;

    /**
     * This attribute maps to the column isprecordid in the jricprecordip table.
     */
    @Column
    protected int isprecordid;

    /**
     * This attribute maps to the column ipid in the jricprecordip table.
     */
    @Column
    protected int ipid;

    /**
     * This attribute maps to the column startip in the jricprecordip table.
     */
    @Column
    protected String startip;

    /**
     * This attribute maps to the column startiplong in the jricprecordip table.
     */
    @Column
    protected long startiplong;

    /**
     * This attribute maps to the column endip in the jricprecordip table.
     */
    @Column
    protected String endip;

    /**
     * This attribute maps to the column endiplong in the jricprecordip table.
     */
    @Column
    protected long endiplong;

    /**
     * Method 'Jricprecordip'
     * 
     */
    public Jricprecordip() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    /**
     * Method 'getJricprecordid'
     * 
     * @return int
     */
    public int getJricprecordid() {
        return jricprecordid;
    }

    /**
     * Method 'setJricprecordid'
     * 
     * @param jricprecordid
     */
    public void setJricprecordid(int jricprecordid) {
        this.jricprecordid = jricprecordid;
    }

    /**
     * Method 'getIsprecordid'
     * 
     * @return int
     */
    public int getIsprecordid() {
        return isprecordid;
    }

    /**
     * Method 'setIsprecordid'
     * 
     * @param isprecordid
     */
    public void setIsprecordid(int isprecordid) {
        this.isprecordid = isprecordid;
    }

    /**
     * Method 'getStartip'
     * 
     * @return String
     */
    public String getStartip() {
        return startip;
    }

    /**
     * Method 'setStartip'
     * 
     * @param startip
     */
    public void setStartip(String startip) {
        this.startip = startip;
    }

    /**
     * Method 'getStartiplong'
     * 
     * @return long
     */
    public long getStartiplong() {
        return startiplong;
    }

    /**
     * Method 'setStartiplong'
     * 
     * @param startiplong
     */
    public void setStartiplong(long startiplong) {
        this.startiplong = startiplong;
    }

    /**
     * Method 'getEndip'
     * 
     * @return String
     */
    public String getEndip() {
        return endip;
    }

    /**
     * Method 'setEndip'
     * 
     * @param endip
     */
    public void setEndip(String endip) {
        this.endip = endip;
    }

    /**
     * Method 'getEndiplong'
     * 
     * @return long
     */
    public long getEndiplong() {
        return endiplong;
    }

    /**
     * Method 'setEndiplong'
     * 
     * @param endiplong
     */
    public void setEndiplong(long endiplong) {
        this.endiplong = endiplong;
    }

    /**
     * Method 'equals'
     * 
     * @param _other
     * @return boolean
     */
    public boolean equals(Object _other) {
        if (_other == null) {
            return false;
        }

        if (_other == this) {
            return true;
        }

        if (!(_other instanceof Jricprecordip)) {
            return false;
        }

        final Jricprecordip _cast = (Jricprecordip) _other;
        if (id != _cast.id) {
            return false;
        }

        if (jricprecordid != _cast.jricprecordid) {
            return false;
        }

        if (isprecordid != _cast.isprecordid) {
            return false;
        }

        if (ipid != _cast.ipid) {
            return false;
        }

        if (startip == null ? _cast.startip != startip : !startip.equals
(_cast.startip)) {
            return false;
        }

        if (startiplong != _cast.startiplong) {
            return false;
        }

        if (endip == null ? _cast.endip != endip : !endip.equals
(_cast.endip)) {
            return false;
        }

        if (endiplong != _cast.endiplong) {
            return false;
        }

        return true;
    }

    /**
     * Method 'hashCode'
     * 
     * @return int
     */
    public int hashCode() {
        int _hashCode = 0;
        _hashCode = 29 * _hashCode + id;
        _hashCode = 29 * _hashCode + jricprecordid;
        _hashCode = 29 * _hashCode + isprecordid;
        _hashCode = 29 * _hashCode + ipid;
        if (startip != null) {
            _hashCode = 29 * _hashCode + startip.hashCode();
        }

        _hashCode = 29 * _hashCode + (int) (startiplong ^ (startiplong >>> 
32));
        if (endip != null) {
            _hashCode = 29 * _hashCode + endip.hashCode();
        }

        _hashCode = 29 * _hashCode + (int) (endiplong ^ (endiplong >>> 32));
        return _hashCode;
    }

    /**
     * Method 'toString'
     * 
     * @return String
     */
    public String toString() {
        StringBuffer ret = new StringBuffer();
        ret.append("com.mycompany.myapp.dto.Jricprecordip: ");
        ret.append("id=" + id);
        ret.append(", jricprecordid=" + jricprecordid);
        ret.append(", isprecordid=" + isprecordid);
        ret.append(", ipid=" + ipid);
        ret.append(", startip=" + startip);
        ret.append(", startiplong=" + startiplong);
        ret.append(", endip=" + endip);
        ret.append(", endiplong=" + endiplong);
        return ret.toString();
    }

    public int getIpid() {
        return ipid;
    }

    public void setIpid(int ipid) {
        this.ipid = ipid;
    }

    public void setId(Integer id) {
        this.id = id;
    } 

对应的表结构:
Column name          Type                                    Nulls

id                   serial                                  no
jricprecordid        integer                                 no
isprecordid          integer                                 no
ipid                 integer                                 no
startip              char(20)                                no
startiplong          decimal(10,0)                           no
endip                char(20)                                no
endiplong            decimal(10,0)                           no

Original comment by Toni.xu...@gmail.com on 10 Mar 2010 at 5:05

GoogleCodeExporter commented 9 years ago
数据库:Informix Dynamic Server 2000 Version 9.21.UC2 

Original comment by Toni.xu...@gmail.com on 10 Mar 2010 at 5:08

GoogleCodeExporter commented 9 years ago
我反对 r916修改:
if (field.getType() != value.getClass())
起码用 equal啊, 再说, 这个应该使用 isAssginFrom

Original comment by wendal1985@gmail.com on 10 Mar 2010 at 5:18

GoogleCodeExporter commented 9 years ago
sorry, 是 Class.isAssignableFrom(Class clz)

Original comment by wendal1985@gmail.com on 10 Mar 2010 at 5:21

GoogleCodeExporter commented 9 years ago
我的修改体现在 r918

Original comment by wendal1985@gmail.com on 10 Mar 2010 at 5:25

GoogleCodeExporter commented 9 years ago
我刚跟同事沟通,发现他的代码那边没有出现这种问题,我��
�我的JAVA工程里面的JAR包跟同事同步成一样的 
发现就不存在这个问题了。。 现在问题已解决了 
我怀疑JAR间有冲突。。很遗憾 我之前没有记下我的旧的工
程里面 有什么JAR包

Original comment by Toni.xu...@gmail.com on 10 Mar 2010 at 6:41

GoogleCodeExporter commented 9 years ago
如果是这样的话,我怀疑是 Castors 
根本没有加载上,为了以后能更清晰的暴露这个问题,每次��
�不到 Castors 
是,抛出的 Exception 应该带着 Castors 的 map.size()

Fixed in r919

Append "FAQ"

Original comment by zozoh...@gmail.com on 10 Mar 2010 at 7:03

GoogleCodeExporter commented 9 years ago

Original comment by wendal1985@gmail.com on 15 Mar 2010 at 5:07

GoogleCodeExporter commented 9 years ago

Original comment by wendal1985@gmail.com on 13 May 2010 at 11:03

GoogleCodeExporter commented 9 years ago

Original comment by zozoh...@gmail.com on 14 May 2010 at 10:39