英文:
Could not initialize class util.HibernateUtil
问题
我尝试运行WS时遇到了这个错误
找不到任何解决方案,如果有人可以帮助我
在Netbeans上运行Hibernate 4.3
HibernateUtil.java
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// 从标准配置文件(hibernate.cfg.xml)创建SessionFactory。
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// 记录异常。
System.err.println("初始化SessionFactory失败。" + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/vendas?useTimeZone=true;useSSL=false;serverTimezone=UTC;autoReconnect=true</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<mapping class="entity.Cliente"/>
</session-factory>
</hibernate-configuration>
ClienController.java
package api;
import entity.Cliente;
import java.util.List;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import org.hibernate.Query;
import org.hibernate.Session;
import util.HibernateUtil;
@Path("clientes")
public class ClientController {
@Context
private UriInfo context;
public ClientController() {
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getTeste() {
Cliente c = new Cliente();
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Query select = session.createQuery("from Cliente");
List<Cliente> objetos = select.list();
objetos.forEach(item -> {
System.out.println("name: " + item.getNomeCliente());
});
return "";
}
}
也许服务器上缺少一些JAR包?
运行在GlassFish 4.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
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/vendas?useTimeZone=true;useSSL=false;serverTimezone=UTC;autoReconnect=true</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<mapping class="entity.Cliente"/>
</session-factory>
</hibernate-configuration>
ClienController.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package api;
import entity.Cliente;
import java.util.List;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import org.hibernate.Query;
import org.hibernate.Session;
import util.HibernateUtil;
/**
* REST Web Service
*
* @author lucas
*/
@Path("clientes")
public class ClientController {
@Context
private UriInfo context;
public ClientController() {
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getTeste() {
Cliente c = new Cliente();
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Query select = session.createQuery("from Cliente");
List<Cliente> objetos = select.list();
objetos.forEach(item -> {
System.out.println("name: " + item.getNomeCliente());
});
return "";
}
}
Maybe some missing jar on server?
Running on GlassFish 4.1.1
Server returns the message
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.
专注分享java语言的经验与见解,让所有开发者获益!
评论