这是我的servlet类 这里设置我的hibernate会话并将对象发送到客户端类,其中是get和set类,并调用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)
这是clientesdao修复Clientes.java参数时的代码
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)
类创建表但在进入表
的值时有错误答案 0 :(得分:2)
public ClienteDAO() {
this.session = session;
}
根据您提供的信息,问题似乎session
变量为null
。
你需要将hibernate session
传递给构造函数作为参数(或)使用setter;
答案 1 :(得分:1)
您的会话为空,您从不设置它
public ClienteDAO(Session session) {
this.session = session;
}
并在客户端代码中
// call the class
Clientes clientes = new Clientes();
ClienteDAO clientedao = new ClienteDAO(session);
顺便说一下,我知道你会说西班牙语,所以班级名称Clientes应该是Cliente总是单数,代表一个实体。