1. 程式人生 > >Hibernate中find()方法問題??這是為什麼?

Hibernate中find()方法問題??這是為什麼?

我在用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: 無效數字