我找到了一个特殊的解决方案,我目前面对的问题是我的代码,但没有找到。我知道有人在这里可以提供帮助。我需要帮助。我在数据库中有一个表记录城市。每个城市必须只属于一个国家,但不同国家的不同城市实际上可能具有相同的名称。我通过表单成功地将一个城市添加到数据库,但我的问题是当我从数据库中检索其中一个记录并尝试更新它时,表单没有对该国家/地区属性进行数据绑定。我尝试打印到控制台,它说国家ID为空。以下是代码 我的城市控制器代码:
@Controller
@RequestMapping("/city")
public class CityController {
@Autowired
private CountriesService countryService;
@Autowired
private CitiesService cityService;
@RequestMapping("/manage")
public String manageCity(Model model){
return"ManageCities";
}
@RequestMapping(value="/add",method=RequestMethod.GET)
public String createCity(Model model){
List<Countries> countryList=new ArrayList<Countries>();
List<String> countryString=new ArrayList<>();
countryString.add("Japan");
model.addAttribute("cityModel", new Cities());
model.addAttribute("countryList", countryService.searchAll());
model.addAttribute("countryString",countryString);
return"AddCity";
}
@RequestMapping(value="/post",method=RequestMethod.POST)
public String postCity(@ModelAttribute("cityModel")Cities city){
cityService.create(city);
return"redirect:/city/manage";
}
@RequestMapping(value="/update",method=RequestMethod.GET)
public String getCities(Model model){
model.addAttribute("cityList", cityService.searchAll());
return"UpdateCity";
}
@RequestMapping(value="/update/{cityId}",method=RequestMethod.GET)
public String getCityUpdateInfo(Model model,@PathVariable("cityId")Integer cityId,HttpSession session){
session.setAttribute("cityId", cityId);
model.addAttribute("cityModel", new Cities());
model.addAttribute("cityObj", cityService.searchById(cityId));
model.addAttribute("countryList",countryService.searchAll());
return"CityInfo";
}
@RequestMapping(value="/update/{cityId}",method=RequestMethod.POST)
public String updateCityUpdate(@ModelAttribute("cityModel")Cities city,HttpSession session){
Integer i=(Integer)session.getAttribute("cityId");
city.setCityCode(i);
System.out.println(city);
System.out.println(city.getCountryCode().toString());
cityService.update(city);
return "redirect:/city/update";
}
}
下面的是运行更新的查询:
@Repository
public class CitiesDaoImpl implements CitiesDao{
@PersistenceContext(unitName = "AirlineReservationSystemPU")
EntityManager em;
@Override
public void create(Cities a) {
em.merge(a);
}
@Override
public void update(Cities a) {
Query query=em.createQuery("update Cities c set c.cityName=:cityName, c.countryCode.countryCode=:countryCode"
+ "where c.cityCode=:cityCode");
query.setParameter("cityName", a.getCityName());
query.setParameter("countryCode", a.getCountryCode().getCountryCode());
query.setParameter("cityCode", a.getCityCode());
query.executeUpdate();
}
@Override
public void delete(Integer a) {
Cities o=searchById(a);
if(o!=null){
em.remove(o);
}
}
@Override
public Cities searchById(Integer Id) {
return em.find(Cities.class, Id);
}
@Override
public List<Cities> searchAll() {
Query query=em.createQuery("SELECT c From Cities c");
List list=query.getResultList();
return list;
}
}
下面的是jsp表单:
<%--
Document : AddCountry
Created on : 23-May-2016, 9:15:38 AM
Author : lami
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<meta name="theme-color" content="#3e454c">
<title>Admin</title>
<!-- Font awesome -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/font-awesome.min.css">
<!-- Sandstone Bootstrap CSS -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/bootstrap.min.css">
<!-- Bootstrap Datatables -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/dataTables.bootstrap.min.css">
<!-- Bootstrap social button library -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/bootstrap-social.css">
<!-- Bootstrap select -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/bootstrap-select.css">
<!-- Bootstrap file input -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/fileinput.min.css">
<!-- Awesome Bootstrap checkbox -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/awesome-bootstrap-checkbox.css">
<!-- Admin Stye -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/styles.css">
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<%@include file="jspincludehead.jsp" %>
<div class="ts-main-content">
<%@include file="jspincludeMenu.jsp" %>
<div class="content-wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<h2 class="page-title">Add City</h2>
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">City Add Form</div>
<div class="panel-body">
<spring:url value="/city/update/{cityId}" var="city"/>
<form:form action="${city}" method="post" class="form-horizontal" modelAttribute="cityModel">
<div class="form-group">
<label class="col-sm-3 control-label">City Name</label>
<div class="col-sm-9">
<form:input path="cityName" type="text" class="form-control" value="${cityObj.getCityName()}" />
</div>
</div>
<div class="hr-dashed"></div>
<div class="form-group">
<label class="col-sm-3 control-label">Select Country</label>
<div class="col-sm-9">
<form:select path="countryCode.countryCode" class="selectpicker" >
<c:forEach items="${countryList}" var="st" varStatus="status">
<c:choose>
<c:when test="${st.getCountryCode() eq cityObj.getCountryCode().getCountryCode()}">
<option value="<c:out value="${st.getCountryCode()}"/>" selected="true">
<c:out value="${st.toString()}"/>
<option>
</c:when>
<c:otherwise>
<option value="<c:out value="${st.getCountryCode()}"/>">
<c:out value="${st.toString()}"/>
</option>
</c:otherwise>
</c:choose>
</c:forEach>
</form:select>
</div>
</div>
<div class="hr-dashed"></div>
<div class="form-group">
<div class="col-sm-3 col-sm-offset-9">
<form:button class="btn btn-primary" type="submit">Update City</form:button></div>
</div>
</form:form>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="clearfix pt pb">
<div class="col-md-12">
</div>
</div>
</div>
</div>
</div>
</div>
<script src="${pageContext.request.contextPath}/resources/js/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/resources/js/bootstrap-select.min.js"></script>
<script src="${pageContext.request.contextPath}/resources/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/resources/js/jquery.dataTables.min.js"></script>
<script src="${pageContext.request.contextPath}/resources/js/dataTables.bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/resources/js/Chart.min.js"></script>
<script src="${pageContext.request.contextPath}/resources/js/fileinput.js"></script>
<script src="${pageContext.request.contextPath}/resources/js/chartData.js"></script>
<script src="${pageContext.request.contextPath}/resources/js/mains.js"></script>
<script>
window.onload = function () {
// Line chart from swirlData for dashReport
var ctx = document.getElementById("dashReport").getContext("2d");
window.myLine = new Chart(ctx).Line(swirlData, {
responsive: true,
scaleShowVerticalLines: false,
scaleBeginAtZero: true
});
// Pie Chart from doughutData
var doctx = document.getElementById("chart-area3").getContext("2d");
window.myDoughnut = new Chart(doctx).Pie(doughnutData, {responsive: true});
// Dougnut Chart from doughnutData
var doctx = document.getElementById("chart-area4").getContext("2d");
window.myDoughnut = new Chart(doctx).Doughnut(doughnutData, {responsive: true});
}
</script>
</body>
每次点击更新按钮都会抛出此错误:
java.lang.IllegalArgumentException: node to traverse cannot be null!
org.hibernate.hql.internal.ast.util.NodeTraverser.traverseDepthFirst(NodeTraverser.java:64)
org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:300)
org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203)
org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1796)
org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)
org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:257)
com.sun.proxy.$Proxy1368.createQuery(Unknown Source)
com.ars.application.dao.AirportsDaoImpl.update(AirportsDaoImpl.java:31)
com.ars.application.service.AirportsServiceImpl.update(AirportsServiceImpl.java:33)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
com.sun.proxy.$Proxy1373.update(Unknown Source)
com.ars.application.controllers.AirportController.postAirportInfo(AirportController.java:77)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
我尝试打印到控制台并意识到弹簧没有将countryCode字段绑定到实体。欢迎任何可能的咳嗽。感谢