用Hibernate往mysql 5.1数据库中进行查询数据,表的结构使用的是utf-8编码,服务器在Linux上,这个时候往数据库上查询,插入没问题。后来往5.1的数据库导入模型的时候,外键不见了,于是在Windows本地安装了一个mysql 5.5的数据库,导入模型正常。但是进行查询的时候抛出异常说
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?? as ??0_, owner0_.??? as ???0_, owner0_.?? as ??0_, owner0_.???? as ????0_ fro' at line 1
但是上面的Hibernate自动生成的sql语句显示不会乱码,当然,这两者没关系,表示我的这个sql语句是没有问题的。
于是我在Windows的mysql5.1中建立一个同样的数据库,发现也是这样的错误。
尝试尝试是否是编码的问题,我检查数据库都是用UTF-8编码设置的,没发现有什么异常。
最后在hibernate.cfg.xml上的session-factory中添加编码设置,最后的文件配置是这样的。
jdbc:mysql://localhost:3306/test
root
password
com.mysql.jdbc.Driver
org.hibernate.dialect.MySQLDialect
true
UTF-8
true