如何在hibernate查询中发布集合名称

fbcarpbf  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(238)

我现在有个问题。我正在通过azure使用clear db。我无法将utf8字符存储到表中。我使用的是spring+hibernate+mysql组合。
我可以通过更新 my.cnf 但在azure上它不允许我更新,因为我使用的是communityedition,并且被许多用户使用。他们建议遵循下面的观点。但是我不知道如何在hibernate配置上设置它
每次打开到数据库的连接时,在执行要在该连接上运行的任何查询之前,应使用中解释的语法发出set names查询https://dev.mysql.com/doc/refman/5.5/en/set-names.html. 例如,要设置所需的字符集和排序规则,可以使用

  1. SET NAMES utf8 COLLATE utf8_bin;

我的休眠文件如下

  1. @Configuration
  2. @EnableTransactionManagement
  3. public class HibernateConfig {
  4. /**
  5. * Uses an datasource library for local testing with out creating datasources on the server. Not to be used for Production.
  6. *
  7. * @return
  8. */
  9. @Bean
  10. public DriverManagerDataSource jndiDataSource() {
  11. DriverManagerDataSource dataSource = new DriverManagerDataSource();
  12. try {
  13. DriverManager.registerDriver(new com.mysql.jdbc.Driver());
  14. } catch (SQLException e) {
  15. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  16. }
  17. dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  18. dataSource.setUrl("jdbc:mysql://<azureurl>-f.cloudapp.net:3306/db1");
  19. dataSource.setUsername("user");
  20. dataSource.setPassword("pw");
  21. return dataSource;
  22. }
  23. /**
  24. * Datasource to be used when running on App servers
  25. * @return
  26. * @throws javax.naming.NamingException
  27. */
  28. /*public @Bean
  29. DataSource jndiDataSource() throws NamingException{
  30. return (DataSource) new InitialContext().lookup(environment.getProperty("datasourceJndi"));
  31. }
  32. * /
  33. /**
  34. * @return
  35. * @throws javax.naming.NamingException
  36. */
  37. @Bean
  38. public org.springframework.orm.hibernate4.LocalSessionFactoryBean sessionFactory() throws NamingException {
  39. LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
  40. Properties prop = new Properties();
  41. prop.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
  42. //prop.put("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
  43. //Be extremely careful before changing this value to Update or Create
  44. prop.put("hibernate.hbm2ddl.auto", "none");
  45. prop.put("hibernate.show_sql", "false");
  46. prop.put("hibernate.enable_lazy_load_no_trans","true");
  47. prop.put("hibernate.connection.CharSet","utf8");
  48. prop.put("hibernate.connection.characterEncoding","utf8");
  49. prop.put("hibernate.connection.useUnicode",true);
  50. sessionFactory.setDataSource(this.jndiDataSource());
  51. sessionFactory.setPackagesToScan(new String[]{"com.sp.domain"});
  52. sessionFactory.setHibernateProperties(prop);
  53. return sessionFactory;
  54. }
  55. /**
  56. * @return
  57. * @throws javax.naming.NamingException
  58. */
  59. @Bean
  60. public HibernateTransactionManager transactionManager() throws NamingException {
  61. HibernateTransactionManager txManager = new HibernateTransactionManager();
  62. txManager.setDataSource(this.jndiDataSource());
  63. txManager.setSessionFactory(this.sessionFactory().getObject());
  64. return txManager;
  65. }
  66. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题