无法初始化类 util.HibernateUtil。

huangapple 未分类评论58阅读模式
英文:

Could not initialize class util.HibernateUtil

问题

我尝试运行WS时遇到了这个错误
找不到任何解决方案,如果有人可以帮助我

在Netbeans上运行Hibernate 4.3

HibernateUtil.java

  1. import org.hibernate.SessionFactory;
  2. import org.hibernate.cfg.Configuration;
  3. public class HibernateUtil {
  4. private static final SessionFactory sessionFactory;
  5. static {
  6. try {
  7. // 从标准配置文件(hibernate.cfg.xml)创建SessionFactory。
  8. sessionFactory = new Configuration().configure().buildSessionFactory();
  9. } catch (Throwable ex) {
  10. // 记录异常。
  11. System.err.println("初始化SessionFactory失败。" + ex);
  12. throw new ExceptionInInitializerError(ex);
  13. }
  14. }
  15. public static SessionFactory getSessionFactory() {
  16. return sessionFactory;
  17. }
  18. }

hibernate.cfg.xml

  1. <hibernate-configuration>
  2. <session-factory>
  3. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  4. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  5. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/vendas?useTimeZone=true;useSSL=false;serverTimezone=UTC;autoReconnect=true</property>
  6. <property name="hibernate.connection.username">root</property>
  7. <property name="hibernate.connection.password">root</property>
  8. <mapping class="entity.Cliente"/>
  9. </session-factory>
  10. </hibernate-configuration>

ClienController.java

  1. package api;
  2. import entity.Cliente;
  3. import java.util.List;
  4. import javax.ws.rs.Produces;
  5. import javax.ws.rs.GET;
  6. import javax.ws.rs.Path;
  7. import javax.ws.rs.PathParam;
  8. import javax.ws.rs.core.Context;
  9. import javax.ws.rs.core.MediaType;
  10. import javax.ws.rs.core.UriInfo;
  11. import org.hibernate.Query;
  12. import org.hibernate.Session;
  13. import util.HibernateUtil;
  14. @Path("clientes")
  15. public class ClientController {
  16. @Context
  17. private UriInfo context;
  18. public ClientController() {
  19. }
  20. @GET
  21. @Produces(MediaType.APPLICATION_JSON)
  22. public String getTeste() {
  23. Cliente c = new Cliente();
  24. Session session = HibernateUtil.getSessionFactory().openSession();
  25. session.beginTransaction();
  26. Query select = session.createQuery("from Cliente");
  27. List<Cliente> objetos = select.list();
  28. objetos.forEach(item -> {
  29. System.out.println("name: " + item.getNomeCliente());
  30. });
  31. return "";
  32. }
  33. }

也许服务器上缺少一些JAR包?
运行在GlassFish 4.1.1上

服务器返回以下消息

  1. org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: Could not initialize class util.HibernateUtil

请帮我解决这个问题,谢谢。

英文:

I'm getting this error when I try to run my WS
Couldn't find any solutions, if anyone could help me

Running Hibernate 4.3 on Netbeans

HibernateUtil.java

  1. import org.hibernate.SessionFactory;
  2. import org.hibernate.cfg.Configuration;
  3. public class HibernateUtil {
  4. private static final SessionFactory sessionFactory;
  5. static {
  6. try {
  7. // Create the SessionFactory from standard (hibernate.cfg.xml)
  8. // config file.
  9. sessionFactory = new Configuration().configure().buildSessionFactory();
  10. } catch (Throwable ex) {
  11. // Log the exception.
  12. System.err.println(&quot;Initial SessionFactory creation failed.&quot; + ex);
  13. throw new ExceptionInInitializerError(ex);
  14. }
  15. }
  16. public static SessionFactory getSessionFactory() {
  17. return sessionFactory;
  18. }
  19. }

hibernate.cfg.xml

  1. &lt;hibernate-configuration&gt;
  2. &lt;session-factory&gt;
  3. &lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
  4. &lt;property name=&quot;hibernate.connection.driver_class&quot;&gt;com.mysql.jdbc.Driver&lt;/property&gt;
  5. &lt;property name=&quot;hibernate.connection.url&quot;&gt;jdbc:mysql://localhost:3306/vendas?useTimeZone=true;useSSL=false;serverTimezone=UTC;autoReconnect=true&lt;/property&gt;
  6. &lt;property name=&quot;hibernate.connection.username&quot;&gt;root&lt;/property&gt;
  7. &lt;property name=&quot;hibernate.connection.password&quot;&gt;root&lt;/property&gt;
  8. &lt;mapping class=&quot;entity.Cliente&quot;/&gt;
  9. &lt;/session-factory&gt;
  10. &lt;/hibernate-configuration&gt;

ClienController.java

  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package api;
  7. import entity.Cliente;
  8. import java.util.List;
  9. import javax.ws.rs.Produces;
  10. import javax.ws.rs.GET;
  11. import javax.ws.rs.Path;
  12. import javax.ws.rs.PathParam;
  13. import javax.ws.rs.core.Context;
  14. import javax.ws.rs.core.MediaType;
  15. import javax.ws.rs.core.UriInfo;
  16. import org.hibernate.Query;
  17. import org.hibernate.Session;
  18. import util.HibernateUtil;
  19. /**
  20. * REST Web Service
  21. *
  22. * @author lucas
  23. */
  24. @Path(&quot;clientes&quot;)
  25. public class ClientController {
  26. @Context
  27. private UriInfo context;
  28. public ClientController() {
  29. }
  30. @GET
  31. @Produces(MediaType.APPLICATION_JSON)
  32. public String getTeste() {
  33. Cliente c = new Cliente();
  34. Session session = HibernateUtil.getSessionFactory().openSession();
  35. session.beginTransaction();
  36. Query select = session.createQuery(&quot;from Cliente&quot;);
  37. List&lt;Cliente&gt; objetos = select.list();
  38. objetos.forEach(item -&gt; {
  39. System.out.println(&quot;name: &quot; + item.getNomeCliente());
  40. });
  41. return &quot;&quot;;
  42. }
  43. }

Maybe some missing jar on server?
Running on GlassFish 4.1.1

Server returns the message

  1. org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: Could not initialize class util.HibernateUtil

Please help me to sort out this issue thanks

答案1

得分: 0

你不应该需要导入util.HibernateUtil。你可以将HibernateUtil类声明为静态,例如public static class HibernateUtils{,然后通过HibernateUtil.doAThing();来使用,或者实例化一个HibernateUtil对象,例如HibernateUtil hibUtil = new HibernateUtil();,然后使用hibUtil.doAThing();。然而,对于后一种情况,你需要在HibernateUtil类中添加一个构造函数。

英文:

You shouldn't need to import util.HibernateUtil. You may need to either declare the HibernateUtil class as static eg public static class HibernateUtils{ and then using by writing HibernateUtil.doAThing(); or instantiate a HibernateUtil Object eg HibernateUtil hibUtil = new HibernateUtil(); and use hibUtil.doAThing(); instead.For the latter though you'll need to include a Constructor to your HibernateUtil class.

huangapple
  • 本文由 发表于 2020年7月27日 12:10:32
  • 转载请务必保留本文链接:https://java.coder-hub.com/63108591.html
匿名

发表评论

匿名网友

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

确定