gpt4 book ai didi

java - 我有 hibernate 错误

转载 作者:太空宇宙 更新时间:2023-11-03 12:26:12 25 4
gpt4 key购买 nike

这是我的 servlet 类在这里设置我的 hibernate session 并将对象发送到客户端类,其中获取和设置类并调用 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)

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

最佳答案

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

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

您需要将 hibernate session 作为参数传递给构造函数(或)使用 setter;

关于java - 我有 hibernate 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17518553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com