gpt4 book ai didi

java - 在 spring MVC 4 中插入约束外键

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:39:00 26 4
gpt4 key购买 nike

我有如下两列的表格

 Field         | Type    | Null | Key | Default | Extra          |
+---------------+---------+------+-----+---------+----------------+
| hakemus_id | int(11) | NO | PRI | NULL | auto_increment |
| fk_duuni_id | int(11) | NO | MUL | NULL | |
| fk_ehdokas_id | int(11) | NO | MUL | NULL | |

我在 Dao 类中有这样的插入方法

public void insertHakemus(Hakemus hakemus) {
String sql="insert into hakemus(fk_duuni_id, fk_ehdokas_id) values(?,?)";
int ehdokas_id= hakemus.getEhdokas_id();
int duuni_id = hakemus.getDuuni_id();
Object[] ob = new Object[]{duuni_id,ehdokas_id};
jdbcTemplate.update(sql, ob);

}

在 Controller 中,我通过使用注解@RequestParam 从 View 中获取 duuni_id 和 ehdokas_id 从 View 中获取参数

@RequestMapping(value="sendCV.do", method=RequestMethod.POST)
public String receiveCV(@RequestParam(value="duuni_id") Integer duuni_id,@RequestParam(value="ehdokas_id") Integer ehdokas_id, @ModelAttribute(value="ehdokas") EhdokasImpl ehdokas, Model model){
Hakemus hakemus = new HakemusImpl();
hakemus.setEhdokas_id(ehdokas_id);
hakemus.setDuuni_id(duuni_id);
System.out.println(duuni_id);
System.out.println(ehdokas_id);
if(!ehdokas_id.equals(0) && !duuni_id.equals(0)){
ehdokasdao.editEhdokas(ehdokas);
hakemusdao.insertHakemus(hakemus);
return "redirect:tyopaikat.do";
}

我使用两个系统打印机来检查 duuni_id 和 ehdokas_id 是否为空,我得到的结果是好的。这意味着参数的值从 View 成功传递到 Controller。但是当我运行这段代码时,我在 insertHakemus() 方法中得到错误 java.lang.NullPointerException,有没有人有任何想法?!

堆栈跟踪

Apr 13, 2016 7:10:16 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/vjb.de] is completed
6
1
Apr 13, 2016 7:12:16 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [vietjob] in context with path [/vjb.de] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at vjb.de.vietjob.dao.hakemusDAO.HakemusDaoImpl.insertHakemus(HakemusDaoImpl.java:29)
at vjb.de.vietjob.controller.HakemusController.receiveCV(HakemusController.java:114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:833)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

bean 类

package vjb.de.vietjob.bean.hakemus;

public class HakemusImpl implements Hakemus {
private int hakemus_id;
private int duuni_id;
private int ehdokas_id;
public int getHakemus_id() {
return hakemus_id;
}
public void setHakemus_id(int hakemus_id) {
this.hakemus_id = hakemus_id;
}
public int getDuuni_id() {
return duuni_id;
}
public void setDuuni_id(int duuni_id) {
this.duuni_id = duuni_id;
}
public int getEhdokas_id() {
return ehdokas_id;
}
public void setEhdokas_id(int ehdokas_id) {
this.ehdokas_id = ehdokas_id;
}


}

最佳答案

请检查您的 jdbctemplate,它可能为空。

关于java - 在 spring MVC 4 中插入约束外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36604299/

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