简体   繁体   English

我有休眠错误

[英]I have error with hibernate

This is my servlet class here set my hibernate session and sending the objects to the client class where are the get and set class and call CustomerDAO 这是我的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)

this is the code of clientesdao when recive the parameter of Clientes.java 这是在获取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;
    }

} }

When run the aplication show this error 运行应用程序时显示此错误

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)

the class create table but have error when into values to table 类创建表,但在进入表的值时出错

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

Based on the information you provided, it seems issues is session variable is null . 根据您提供的信息,似乎问题是session变量为null

You need to pass hibernate session to constructor as parameter (or) use setters; 您需要将休眠session作为参数传递给构造函数(或使用setter);

Your session is null ,you are not setting it never 您的会话为空,您永远不会设置它

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

and in client code 并在客户端代码中

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

By the way i know you speak spanish so class name Clientes should be Cliente always in singular, represents an Entity. 顺便说一句,我知道您说西班牙语,所以类名Clientes应该始终是Cliente的单数形式,代表一个实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM