会话工厂在Hibernate中返回为null。

huangapple 未分类评论52阅读模式
标题翻译

SessionFactory returning null in Hibernate

问题

  1. 我是Hibernate的初学者在创建`SessionFactory`时遇到了问题
  2. **SessionFactoryProvider.java**
  3. ```java
  4. public class SessionFactoryProvider {
  5. private static SessionFactory factory;
  6. public static SessionFactory getFactory() {
  7. try {
  8. if (factory == null) {
  9. Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
  10. factory = cfg.buildSessionFactory();
  11. }
  12. } catch (Exception e) {
  13. try {
  14. if (factory == null) {
  15. Configuration configuration = new Configuration();
  16. // Hibernate设置与hibernate.cfg.xml中的属性等效
  17. Properties property = new Properties();
  18. property.put(Environment.DRIVER, "com.mysql.jdbc.Driver");
  19. property.put(Environment.URL, "jdbc:mysql://localhost:3306/shopingsite");
  20. property.put(Environment.USER, "root");
  21. property.put(Environment.PASS, "");
  22. property.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
  23. property.put(Environment.SHOW_SQL, "true");
  24. property.put(Environment.HBM2DDL_AUTO, "update");
  25. property.put(Environment.POOL_SIZE, 12);
  26. configuration.setProperties(property);
  27. configuration.addAnnotatedClass(Category.class);
  28. configuration.addAnnotatedClass(User.class);
  29. configuration.addAnnotatedClass(Product.class);
  30. ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
  31. .applySettings(configuration.getProperties()).build();
  32. factory = configuration.buildSessionFactory(serviceRegistry);
  33. }
  34. } catch (HibernateException | NullPointerException he) {
  35. System.out.println("Both factory code Fail");
  36. }
  37. }
  38. return factory;
  39. }
  40. public static void shutdow() {
  41. factory.close();
  42. }
  43. }

pom.xml:

  1. <!-- 在这里是pom.xml的内容 -->

hibernate.cfg.xml:

  1. <!-- 在这里是hibernate.cfg.xml的内容 -->

日志中显示的错误如下:

  1. Info: MavenShopping was successfully deployed in 1,530 milliseconds.
  2. Info: 1
  3. Info: HHH000412: Hibernate Core {5.4.6.Final}
  4. Info: ******************************************
  5. Info: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
  6. WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
  7. Info: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/shopingsite]
  8. Info: HHH10001001: Connection properties: {user=root, password=****, autocommit=true}
  9. Info: HHH10001003: Autocommit mode: true
  10. Warning: StandardWrapperValve[RegistrationServlet]: Servlet.service() for servlet RegistrationServlet threw exception
  11. java.lang.NoSuchMethodError: org.hibernate.internal.CoreMessageLogger.debugf(Ljava/lang/String;I)V

如何解决这个问题?

  1. <details>
  2. <summary>英文翻译</summary>
  3. I am beginner in Hibernate and facing problem in creation of `SessionFactory`,
  4. **SessionFactoryProvider.java :**
  5. public class SessionFactoryProvider {
  6. private static SessionFactory factory;
  7. public static SessionFactory getFactory() {
  8. try {
  9. if (factory == null) {
  10. Configuration cfg = new Configuration().configure(&quot;hibernate.cfg.xml&quot;);
  11. factory = cfg.buildSessionFactory();
  12. }
  13. } catch (Exception e) {
  14. try {
  15. if (factory == null) {
  16. Configuration configuration = new Configuration();
  17. // Hibernate settings equivalent to hibernate.cfg.xml&#39;s properties
  18. Properties property = new Properties();
  19. property.put(Environment.DRIVER, &quot;com.mysql.jdbc.Driver&quot;);
  20. property.put(Environment.URL, &quot;jdbc:mysql://localhost:3306/shopingsite&quot;);
  21. property.put(Environment.USER, &quot;root&quot;);
  22. property.put(Environment.PASS, &quot;&quot;);
  23. property.put(Environment.DIALECT, &quot;org.hibernate.dialect.MySQL5Dialect&quot;);
  24. property.put(Environment.SHOW_SQL, &quot;true&quot;);
  25. property.put(Environment.HBM2DDL_AUTO, &quot;update&quot;);
  26. property.put(Environment.POOL_SIZE, 12);
  27. configuration.setProperties(property);
  28. configuration.addAnnotatedClass(Category.class);
  29. configuration.addAnnotatedClass(User.class);
  30. configuration.addAnnotatedClass(Product.class);
  31. ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
  32. .applySettings(configuration.getProperties()).build();
  33. factory = configuration.buildSessionFactory(serviceRegistry);
  34. }
  35. } catch (HibernateException | NullPointerException he) {
  36. System.out.println(&quot;Both factory code Fail&quot;);
  37. }
  38. }
  39. return factory;
  40. }
  41. public static void shutdow() {
  42. factory.close();
  43. }
  44. }
  45. **pom.xml :**
  46. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  47. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  48. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  49. &lt;groupId&gt;com.mycompany&lt;/groupId&gt;
  50. &lt;artifactId&gt;MavenShopping&lt;/artifactId&gt;
  51. &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  52. &lt;packaging&gt;war&lt;/packaging&gt;
  53. &lt;name&gt;MavenShopping&lt;/name&gt;
  54. &lt;properties&gt;
  55. &lt;endorsed.dir&gt;${project.build.directory}/endorsed&lt;/endorsed.dir&gt;
  56. &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
  57. &lt;/properties&gt;
  58. &lt;build&gt;
  59. &lt;plugins&gt;
  60. &lt;plugin&gt;
  61. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  62. &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
  63. &lt;version&gt;3.1&lt;/version&gt;
  64. &lt;configuration&gt;
  65. &lt;source&gt;1.7&lt;/source&gt;
  66. &lt;target&gt;1.7&lt;/target&gt;
  67. &lt;compilerArguments&gt;
  68. &lt;endorseddirs&gt;${endorsed.dir}&lt;/endorseddirs&gt;
  69. &lt;/compilerArguments&gt;
  70. &lt;/configuration&gt;
  71. &lt;/plugin&gt;
  72. &lt;plugin&gt;
  73. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  74. &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
  75. &lt;version&gt;2.6&lt;/version&gt;
  76. &lt;configuration&gt;
  77. &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
  78. &lt;/configuration&gt;
  79. &lt;/plugin&gt;
  80. &lt;plugin&gt;
  81. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  82. &lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;
  83. &lt;version&gt;2.6&lt;/version&gt;
  84. &lt;executions&gt;
  85. &lt;execution&gt;
  86. &lt;phase&gt;validate&lt;/phase&gt;
  87. &lt;goals&gt;
  88. &lt;goal&gt;copy&lt;/goal&gt;
  89. &lt;/goals&gt;
  90. &lt;configuration&gt;
  91. &lt;outputDirectory&gt;${endorsed.dir}&lt;/outputDirectory&gt;
  92. &lt;silent&gt;true&lt;/silent&gt;
  93. &lt;artifactItems&gt;
  94. &lt;artifactItem&gt;
  95. &lt;groupId&gt;javax&lt;/groupId&gt;
  96. &lt;artifactId&gt;javaee-endorsed-api&lt;/artifactId&gt;
  97. &lt;version&gt;7.0&lt;/version&gt;
  98. &lt;type&gt;jar&lt;/type&gt;
  99. &lt;/artifactItem&gt;
  100. &lt;/artifactItems&gt;
  101. &lt;/configuration&gt;
  102. &lt;/execution&gt;
  103. &lt;/executions&gt;
  104. &lt;/plugin&gt;
  105. &lt;/plugins&gt;
  106. &lt;/build&gt;
  107. &lt;dependencies&gt;
  108. &lt;dependency&gt;
  109. &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
  110. &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
  111. &lt;version&gt;5.4.6.Final&lt;/version&gt;
  112. &lt;type&gt;jar&lt;/type&gt;
  113. &lt;/dependency&gt;
  114. &lt;dependency&gt;
  115. &lt;groupId&gt;mysql&lt;/groupId&gt;
  116. &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
  117. &lt;version&gt;5.1.16&lt;/version&gt;
  118. &lt;/dependency&gt;
  119. &lt;dependency&gt;
  120. &lt;groupId&gt;javax&lt;/groupId&gt;
  121. &lt;artifactId&gt;javaee-web-api&lt;/artifactId&gt;
  122. &lt;version&gt;8.0&lt;/version&gt;
  123. &lt;type&gt;jar&lt;/type&gt;
  124. &lt;/dependency&gt;
  125. &lt;/dependencies&gt;
  126. &lt;/project&gt;
  127. **hibernate.cfg.xml**
  128. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  129. &lt;!DOCTYPE hibernate-configuration PUBLIC &quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot; &quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;
  130. &lt;hibernate-configuration&gt;
  131. &lt;session-factory&gt;
  132. &lt;property name=&quot;hibernate.hbm2ddl.auto&quot;&gt;update&lt;/property&gt;
  133. &lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQL5Dialect&lt;/property&gt;
  134. &lt;property name=&quot;hibernate.connection.url&quot;&gt;jdbc:mysql://localhost:3306/shopingsite&lt;/property&gt;
  135. &lt;property name=&quot;hibernate.connection.username&quot;&gt;root&lt;/property&gt;
  136. &lt;property name=&quot;hibernate.connection.password&quot;/&gt;
  137. &lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;
  138. &lt;property name=&quot;hibernate.connection.autocommit&quot;&gt;true&lt;/property&gt;
  139. &lt;property name=&quot;hibernate.connection.driver_class&quot;&gt;com.mysql.jdbc.Driver&lt;/property&gt;
  140. &lt;mapping class=&quot;BeanFile.Category&quot;/&gt;
  141. &lt;mapping class=&quot;BeanFile.Product&quot;/&gt;
  142. &lt;mapping class=&quot;BeanFile.User&quot;/&gt;
  143. &lt;/session-factory&gt;
  144. &lt;/hibernate-configuration&gt;
  145. Error which was showing in the log is shown below
  146. Info: MavenShopping was successfully deployed in 1,530 milliseconds.
  147. Info: 1
  148. Info: HHH000412: Hibernate Core {5.4.6.Final}
  149. Info: ******************************************
  150. Info: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
  151. WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
  152. Info: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/shopingsite]
  153. Info: HHH10001001: Connection properties: {user=root, password=****, autocommit=true}
  154. Info: HHH10001003: Autocommit mode: true
  155. Warning: StandardWrapperValve[RegistrationServlet]: Servlet.service() for servlet RegistrationServlet threw exception
  156. java.lang.NoSuchMethodError: org.hibernate.internal.CoreMessageLogger.debugf(Ljava/lang/String;I)V
  157. How to solve this ?
  158. </details>
  159. # 答案1
  160. **得分**: 0
  161. 你的[Hibernate SessionFactory][1]存在问题,与你的环境有关。看起来你的编译时库与运行时库不一致,因为在编码时使用的某个类的方法在服务器启动时不存在。
  162. java.lang.NoSuchMethodError: org.hibernate.internal.CoreMessageLogger.debugf(Ljava/lang/String;I)V
  163. 这似乎是导致[SessionFactory创建][2]出现问题的主要原因。
  164. [1]: https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/3-ways-to-build-a-Hibernate-SessionFactory-in-Java-by-example
  165. [2]: https://www.youtube.com/watch?v=Fo11opVImo8
  166. <details>
  167. <summary>英文翻译</summary>
  168. The problem with your [Hibernate SessionFactory][1] has something to do with your environment. It would appear that your compile time libraries are not the same as your runtime libraries, as a method on one of the classes you use when coding isn&#39;t there when the server kicks off.
  169. java.lang.NoSuchMethodError: org.hibernate.internal.CoreMessageLogger.debugf(Ljava/lang/String;I)V
  170. That seems to be the big problem triggering issues with [SessionFactory creation][2].
  171. [1]: https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/3-ways-to-build-a-Hibernate-SessionFactory-in-Java-by-example
  172. [2]: https://www.youtube.com/watch?v=Fo11opVImo8
  173. </details>

huangapple
  • 本文由 发表于 2020年5月30日 20:30:21
  • 转载请务必保留本文链接:https://java.coder-hub.com/62102466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定