Hibernate中find()方法问题??这是为什么?

来源:互联网 发布:漫画软件推荐知乎 编辑:程序博客网 时间:2024/06/05 02:10

我在用find()方法查找的时候传递的是String类型,数据库字段类型roleId是varchar类型. 假如现在有
String id='0001';//实际上应该是从数据库中检索出id
find('  from GlobalRoleModel g where g.roleId='+id);
如果现在数据库中有N条记录 roleId字段在数据库存储的时候只有数字字符,没有其它任何字符.这样查询的时候没有任何错误.问题就出现在这里如果roleId字段有其他字符,就会抛出以下异常.如果此时把find()方法修改为
find('  from GlobalRoleModel g where g.roleId= ''+id+'''); 就不会抛出异常

org.springframework.dao.DataIntegrityViolationException: Hibernate operation: could not execute query; SQL [select globalrole0_.role_id as role1_, globalrole0_.role_name as role2_17_, globalrole0_.role_note as role3_17_ from Global_Role globalrole0_ where globalrole0_.role_id=0005]; ORA-01722: 无效数字
; nested exception is java.sql.SQLException: ORA-01722: 无效数字

Caused by:
java.sql.SQLException: ORA-01722: 无效数字

原创粉丝点击