无法在Hibernate和Spring MVC中添加或更新外键

伊姆·埃萨姆(Yomna esam)

伙计们,我有2个表“ employee”,其中有EMPID,EMPAGE,ADDRESS,SALARY,EMPNAME,department_id(表“ department”的外键引用)和“ department”表,其中department_id,名称……这是我的Department.java

@OneToMany(mappedBy="department",cascade = { CascadeType.ALL },  fetch=FetchType.EAGER, orphanRemoval=true)
private List<Employee> employees;
public List<Employee> getEmployees() {
    return employees;
}
public void setEmployees(List<Employee> employees) {
    this.employees = employees;
}

和Employee.java

@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="department_id")
private Department department;
public Department getDepartment() {
    return department;
}
public void setDepartment(Department department) {
    this.department = department;
}

并为我服务

@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false)
public void addEmployee(String[] list, Employee employee) {
    employee.getDepartment().setDepId(Integer.parseInt(list[3])); // exception in this line
    employee.setEmpName(list[2]);
    employee.setEmpAge(Integer.parseInt(list[4]));
    employee.setEmpAddress(list[6]);
    employee.setSalary(list[1]);
    this.employeeDao.addOrEditEmployee(employee);
}

这是我的错误

Apr 10, 2016 1:27:29 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [sdnext] in context with path [/Spring3HibernateApp1] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
	at com.dineshonjava.service.EmployeeServiceImpl.addEmployee(EmployeeServiceImpl.java:27)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:266)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
	at com.sun.proxy.$Proxy30.addEmployee(Unknown Source)
	at com.dineshonjava.controller.EmployeeController.doPostSave(EmployeeController.java:65)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
	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:781)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

阿卜杜勒哈克

这是employee.getDepartment()错误的问题,因为实例employee仍然为空,getDepartment您不需要setDepartment:

  public void addEmployee(String[] list,Employee employee) {
    employee.setDepartment(..); 
    ...
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法在MySQL中添加外键约束

来自分类Dev

无法在MySQL中添加外键约束

来自分类Dev

无法在phpmyadmin中添加外键约束

来自分类Dev

无法在Laravel中添加外键

来自分类Dev

无法在 MySql 中添加外键

来自分类Dev

无法添加或更新子行:MySQL中的外键约束失败

来自分类Dev

无法添加或更新子行:外键约束失败-休眠中的双向映射

来自分类Dev

无法添加或更新子行:在foreach中执行INSERT时,外键约束失败

来自分类Dev

无法添加或更新子行:magetno中的外键约束失败

来自分类Dev

无法将元素添加到模型类的 ICollection 或更新数据库中的外键

来自分类Dev

如何在django模型的外键字段中添加和更新数据

来自分类Dev

外键无法添加

来自分类Dev

无法添加外键

来自分类Dev

MySQL外键约束-无法添加或更新子行

来自分类Dev

无法添加或更新子行:外键约束失败

来自分类Dev

无法添加或更新子行:违反外键

来自分类Dev

无法添加或更新子行:MySQL外键

来自分类Dev

无法添加或更新子行:外键约束失败

来自分类Dev

无法添加或更新子行:外键约束失败

来自分类Dev

Spring无法添加外键约束@ManyToOne

来自分类Dev

Spring mysql Hibernate中的外键参考

来自分类Dev

无法使用spring-mvc和hibernate从数据库中获取数据

来自分类Dev

在peewee中创建新模型实例时如何修复“无法添加或更新子行:外键约束失败”

来自分类Dev

无法添加或更新子行:外键约束失败(俱乐部和用户)

来自分类Dev

外键更新异常:在Hibernate一对多关系中,外键设置为null

来自分类Dev

使用Hibernate和Spring更新

来自分类Dev

无法在Laravel迁移中添加外键约束

来自分类Dev

无法在Laravel Migration中添加外键约束

来自分类Dev

无法使用java在sqlite中添加外键约束

Related 相关文章

  1. 1

    无法在MySQL中添加外键约束

  2. 2

    无法在MySQL中添加外键约束

  3. 3

    无法在phpmyadmin中添加外键约束

  4. 4

    无法在Laravel中添加外键

  5. 5

    无法在 MySql 中添加外键

  6. 6

    无法添加或更新子行:MySQL中的外键约束失败

  7. 7

    无法添加或更新子行:外键约束失败-休眠中的双向映射

  8. 8

    无法添加或更新子行:在foreach中执行INSERT时,外键约束失败

  9. 9

    无法添加或更新子行:magetno中的外键约束失败

  10. 10

    无法将元素添加到模型类的 ICollection 或更新数据库中的外键

  11. 11

    如何在django模型的外键字段中添加和更新数据

  12. 12

    外键无法添加

  13. 13

    无法添加外键

  14. 14

    MySQL外键约束-无法添加或更新子行

  15. 15

    无法添加或更新子行:外键约束失败

  16. 16

    无法添加或更新子行:违反外键

  17. 17

    无法添加或更新子行:MySQL外键

  18. 18

    无法添加或更新子行:外键约束失败

  19. 19

    无法添加或更新子行:外键约束失败

  20. 20

    Spring无法添加外键约束@ManyToOne

  21. 21

    Spring mysql Hibernate中的外键参考

  22. 22

    无法使用spring-mvc和hibernate从数据库中获取数据

  23. 23

    在peewee中创建新模型实例时如何修复“无法添加或更新子行:外键约束失败”

  24. 24

    无法添加或更新子行:外键约束失败(俱乐部和用户)

  25. 25

    外键更新异常:在Hibernate一对多关系中,外键设置为null

  26. 26

    使用Hibernate和Spring更新

  27. 27

    无法在Laravel迁移中添加外键约束

  28. 28

    无法在Laravel Migration中添加外键约束

  29. 29

    无法使用java在sqlite中添加外键约束

热门标签

归档