0

这是我的 servlet 类,在这里设置我的休眠会话并将对象发送到客户端类,其中获取和设置类并调用 CustomerDAO

// star hibernate session 4.x
    Configuration configuration = new Configuration().configure();
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
    .buildServiceRegistry();
    SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    Session session = sessionFactory.openSession();


    // call the class
    Clientes clientes = new Clientes();
    ClienteDAO clientedao = new ClienteDAO();

 //send parameter to class

    clientes.setNombre("nombre");
    clientes.setApellido("apellido");
    clientes.setTelefono("telefono");
    clientes.setEmail("correo");
    clientes.setPass("clave1");

      //send the parameter to dao
    clientedao.createCliente(clientes);  // error here  at com.utp.soft6.RegistroSv.processRequest(RegistroSv.java:80)

这是接收Clientes.java参数时clientesdao的代码

public class ClienteDAO {

private Session session;

public ClienteDAO() {
     this.session = session;
}



public long createCliente(Clientes cliente) {
    long clienteId = -1;
    Transaction tx = null;
    try {
    tx = session.beginTransaction();  //error here  at com.utp.soft6.model.ClienteDAO.createCliente(ClienteDAO.java:30)

    clienteId = (Long)session.save(cliente);
    tx.commit();
    } catch (Exception e) {

    e.printStackTrace();
    }
    return clienteId;
    }

}

运行应用程序时显示此错误

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
Hibernate: drop table if exists clientes
Hibernate: create table clientes (id bigint not null auto_increment, apellido varchar(255), email varchar(255), nombre varchar(255), pass varchar(255), telefono varchar(255), primary key (id)) ENGINE=InnoDB
java.lang.NullPointerException
    at com.utp.soft6.model.ClienteDAO.createCliente(ClienteDAO.java:30)
    at com.utp.soft6.RegistroSv.processRequest(RegistroSv.java:80)
    at com.utp.soft6.RegistroSv.doGet(RegistroSv.java:124)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

该类创建表但在将值输入表时出错

4

2 回答 2

2
public ClienteDAO() {
     this.session = session;
}

根据您提供的信息,问题似乎是session可变的null

您需要将休眠传递session给构造函数作为参数(或)使用设置器;

于 2013-07-08T03:00:36.813 回答
1

你的会话是空的,你没有设置它永远

public ClienteDAO(Session session) {
     this.session = session;
}

在客户端代码中

// call the class
Clientes clientes = new Clientes();
ClienteDAO clientedao = new ClienteDAO(session);

顺便说一句,我知道你说西班牙语,所以类名 Clientes 应该是 Cliente 总是单数,代表一个实体。

于 2013-07-08T03:00:43.760 回答