NutzCN Logo
精华 Nutz Postgresql 数据库ORM查询失败,但是SqlCallback 却成功
发布于 3066天前 作者 guanml 3915 次浏览 复制 上一个帖子 下一个帖子
标签:

大家有没有遇到 postgresql 数据库 ORM查询报异常,但是用sqlcallback 却成功的问题?
List infos = dao.query(Comminfo.class, Cnd.where("bcdtype", "=", 0), dao.createPager(1, 50));
该查询会报异常

Exception in thread "main" org.nutz.dao.DaoException: !Nutz SQL Error: 'SELECT * FROM COMMINFO  WHERE BCDTYPE=? LIMIT 50 OFFSET 0 
    | 1 |
    |---|
    | 0 |
  For example:> "SELECT * FROM COMMINFO  WHERE BCDTYPE=0 LIMIT 50 OFFSET 0 "'
PreparedStatement: 
'SELECT * FROM COMMINFO  WHERE BCDTYPE=? LIMIT 50 OFFSET 0 '
CaseMessage=错误: 操作符不存在: integer = character varying
  建议:没有匹配指定名称和参数类型的操作符. 您也许需要增加明确的类型转换.
  位置:38
	at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:96)
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2284)
	at org.nutz.dao.impl.DaoSupport$DaoExec.invoke(DaoSupport.java:316)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:38)
	at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:239)
	at org.nutz.dao.impl.DaoSupport$DaoExec.run(DaoSupport.java:306)
	at org.nutz.trans.Trans.exec(Trans.java:174)
	at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:276)
	at org.nutz.dao.impl.NutDao.query(NutDao.java:465)
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2003)
	at com.beyondb.beidou.test.PGQueryTest.main(PGQueryTest.java:51)
Caused by: org.postgresql.util.PSQLException: 错误: 操作符不存在: integer = character varying
  建议:没有匹配指定名称和参数类型的操作符. 您也许需要增加明确的类型转换.
  位置:38
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2284)
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:200)
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2003)
	at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:424)
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:200)
	at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:424)
	at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:161)
	at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:161)
	at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:114)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:228)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:45)
	... 8 more
	at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:114)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:228)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:45)
	at org.nutz.dao.impl.DaoSupport$DaoExec.invoke(DaoSupport.java:316)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:38)
	at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:239)
	at org.nutz.dao.impl.DaoSupport$DaoExec.run(DaoSupport.java:306)
	at org.nutz.trans.Trans.exec(Trans.java:174)
	at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:276)
	at org.nutz.dao.impl.NutDao.query(NutDao.java:465)
	at com.beyondb.beidou.test.PGQueryTest.main(PGQueryTest.java:51)

但是SqlCallback 模式却不会,两者生成的sql应该都是一样的

String sql = "SELECT * FROM COMMINFO  WHERE bcdtype=@bcd LIMIT 50 OFFSET 1";
            Sql  sqls = Sqls.create(sql);
            sqls.params().set("bcd", 0);
            sqls.setCallback(new SqlCallback() {
                @Override
                public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                    int cmmid = 0; 
                    while (rs.next()) {                        
                        cmmid  = rs.getInt("commid");
                        System.out.println(cmmid);
                    }
                    return cmmid;
                }
            });
            
            dao.execute(sqls);
            

这个是数据库的问题,还是 Nutz 的ORM问题?

4 回复

大小写不一样,会不会是这个原因

来自炫酷的 NutzCN

@wendal 我试过统一使用大写的,但是没什么用,依旧报异常。

看上去是类型不匹配,pojo里面声明的是字符串?

来自炫酷的 NutzCN

@wendal 问题解决了,POJO我是用NetBeans 自带的bean生成工具从Oracle 读取表生成pojo的,然后修改成Nutz的注解,后来数据库迁移到POSTGRESQL 就没有注意到Oracle 的NUMBER类型会生成BigInteger 对象,在Oracle数据库下访问是正常的,换成Postgresql 就报错了,我把BigInteger 全部修改成Integer 就正常了。

添加回复
请先登陆
回到顶部