Hibernate4.3.x Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

深藏阁楼爱情的钟 2022-08-01 12:28 279阅读 0赞
  1. 如下错误
  2. Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
  3. 今天没事看看新的hibernate-core-4.3.10.Final 看来官方文档。
  4. public class Person { public Integer getId() {
  5. return id;
  6. }
  7. public void setId(Integer id) {
  8. this.id = id;
  9. }
  10. public String getName() {
  11. return name;
  12. }
  13. public void setName(String name) {
  14. this.name = name;
  15. }
  16. public int getPassword() {
  17. return password;
  18. }
  19. public void setPassword(int password) {
  20. this.password = password;
  21. }
  22. public Date getBirthday() {
  23. return birthday;
  24. }
  25. public void setBirthday(Date birthday) {
  26. this.birthday = birthday;
  27. }
  28. private Integer id;
  29. private String name;
  30. private int password;
  31. private Date birthday;
  32. }
  33. <?xml version="1.0"?>
  34. <!DOCTYPE hibernate-mapping PUBLIC
  35. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  36. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  37. <!-- 持久化映射文件(将java对象映射到数据库表) -->
  38. <hibernate-mapping>
  39. <class name="com.it.hibernate.pojo.Person" table="t_person">
  40. <id name="id">
  41. <generator class="native"></generator>
  42. </id>
  43. <property name="name" column="t_name" length="12"/>
  44. <property name="password" length="6"/>
  45. <property name="birthday" />
  46. </class>
  47. </hibernate-mapping>
  48. 官方获取sessionfactory
  49. public class HibernateUtil {
  50. private static final SessionFactory sessionFactory = buildSessionFactory();
  51. private static SessionFactory buildSessionFactory() {
  52. try {
  53. // Create the SessionFactory from hibernate.cfg.xml
  54. return new Configuration().configure().buildSessionFactory(
  55. new StandardServiceRegistryBuilder().build() );
  56. }
  57. catch (Throwable ex) {
  58. // Make sure you log the exception, as it might be swallowed
  59. System.err.println("Initial SessionFactory creation failed." + ex);
  60. throw new ExceptionInInitializerError(ex);
  61. }
  62. }
  63. public static SessionFactory getSessionFactory() {
  64. return sessionFactory;
  65. }
  66. }
  67. hibernate.cfg.xml 如下
  68. <?xml version='1.0' encoding='utf-8'?>
  69. <!DOCTYPE hibernate-configuration PUBLIC
  70. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  71. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  72. <hibernate-configuration>
  73. <session-factory>
  74. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  75. <property name="hibernate.connection.url">jdbc:mysql:///hibernate_db</property>
  76. <property name="hibernate.connection.username">root</property>
  77. <property name="hibernate.connection.password">123456</property>
  78. <!-- 数据库方言 加五提高速度 -->
  79. <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
  80. <!-- 展现sql -->
  81. <property name="show_sql">true</property>
  82. <!-- 格式化sql -->
  83. <property name="hibernate.format_sql">true</property>
  84. <!-- 自动创建sql create 每次执行都都要先删除表再创建表 update 没有就创建表 有就直接插入 -->
  85. <property name="hibernate.hbm2ddl.auto">update</property>
  86. <mapping resource="com/it/hibernate/pojo/Person.hbm.xml" />
  87. </session-factory>
  88. </hibernate-configuration>
  89. 测试类
  90. public class PersonTest {
  91. public static void main(String[] args) {
  92. SessionFactory factory =HibernateUtil.getSessionFactory();
  93. Session session = factory.openSession();
  94. Transaction tx = session.beginTransaction();
  95. Person p = new Person();
  96. p.setName("MOMO");
  97. p.setPassword(123456);
  98. p.setBirthday(new Date());
  99. session.save(p);
  100. tx.commit();
  101. session.close();
  102. }
  103. }
  104. 如下错误
  105. Initial SessionFactory creation failed.org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
  106. Exception in thread "main" java.lang.ExceptionInInitializerError
  107. at com.it.hibernate.utils.HibernateUtil2.buildSessionFactory(HibernateUtil2.java:21)
  108. at com.it.hibernate.utils.HibernateUtil2.<clinit>(HibernateUtil2.java:9)
  109. at com.it.hibernate.pojo.test.PersonTest.main(PersonTest.java:14)
  110. Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
  111. at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104)
  112. at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71)
  113. at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:209)
  114. at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
  115. at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
  116. at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
  117. at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
  118. at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
  119. at com.it.hibernate.utils.HibernateUtil2.buildSessionFactory(HibernateUtil2.java:16)
  120. ... 2 more
  121. 修改获取sessionFactory的公用方法
  122. public class HibernateUtil {
  123. private static final SessionFactory sessionFactory = buildSessionFactory();
  124. private static SessionFactory buildSessionFactory() {
  125. try {
  126. // 从 hibernate.cfg.xml 创建SessionFactory
  127. Configuration cfg = new Configuration().configure(); StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(cfg.getProperties()).build(); return cfg.buildSessionFactory( serviceRegistry);
  128. }
  129. catch (Throwable ex) {
  130. System.err.println("Initial SessionFactory creation failed." + ex);
  131. throw new ExceptionInInitializerError(ex);
  132. }
  133. }
  134. public static SessionFactory getSessionFactory() {
  135. return sessionFactory;
  136. }
  137. }
  138. 修改完就可以顺利插入数据。 有个疑问是官方给错了?还是我其他地方有问题有问题呢! 但是我改了sessionfactory获取方式就好了呀!求大神指点。
  139. private static final SessionFactory sessionFactory = buildSessionFactory();
  140. private static SessionFactory buildSessionFactory() {
  141. try {
  142. // Create the SessionFactory from hibernate.cfg.xml
  143. return new Configuration().configure().buildSessionFactory(
  144. new StandardServiceRegistryBuilder().build() );
  145. }
  146. catch (Throwable ex) {
  147. // Make sure you log the exception, as it might be swallowed
  148. System.err.println("Initial SessionFactory creation failed." + ex);
  149. throw new ExceptionInInitializerError(ex);
  150. }
  151. }
  152. public static SessionFactory getSessionFactory() {
  153. return sessionFactory;
  154. }
  155. }

发表评论

表情:
评论列表 (有 0 条评论,279人围观)

还没有评论,来说两句吧...

相关阅读