用 Hibernate查询数据库的时候报错如下:
org.hibernate.exception.SQLGrammarException: #42000
....
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: #42000
这个问题也弄了很久才发现问题
可以看一下我console打印出来的sql语句:
登录查询数据库的语句如下:
Hibernate: select ltuser0_.id as id0_, ltuser0_.username as username0_, ltuser0_.userpwd as userpwd0_, ltuser0_.other as other0_ from ltuser ltuser0_ where ltuser0_.username=? and ltuser0_.userpwd=? 这个没有问题可以查询
查询当前用户信息的语句如下:
Hibernate: select ltperson0_.id as id1_, ltperson0_.username as username1_, ltperson0_.pername as pername1_, ltperson0_.perType as perType1_, ltperson0_.peridcard as peridcard1_, ltperson0_.native as native1_, ltperson0_.sex as sex1_, ltperson0_.birthday as birthday1_, ltperson0_.phone as phone1_, ltperson0_.weixin as weixin1_, ltperson0_.address as address1_, ltperson0_.hobby as hobby1_, ltperson0_.state as state1_, ltperson0_.beizhu as beizhu1_
from LT-LIBRARY.ltperson ltperson0_ where ltperson0_.username=?
这个就有问题了,发现了没,多了个数据库名,LT-LIBRARY.ltperson,LT-LIBRARY是我 给数据库起的名字lt-library, 得把这个去掉才不报错。
映射文件都是用工具生成的,所以这个的问题应该就是出现在XXX.hbm.xml文件中,把catalog="LT-LIBRARY"去掉就可以了,就不报这个错。