当我在控制台中运行应用程序时,结果是正确的。父正在更新,孩子不能删除。
app.java
public class app {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:app-context-annotation.xml");
ctx.refresh();
ClientService clientService = ctx.getBean("jpaClientService", ClientService.class);
UserService userService = ctx.getBean("jpaUserService", UserService.class);
CustomerService customerService = ctx.getBean("jpaCustomerService", CustomerService.class);
TroubleService troubleService = ctx.getBean("jpaTroubleService", TroubleService.class);
Client newClient = clientService.findOne(8l);
String newClientName = "IP 8";
newClient.setClient(newClientName);
clientService.saveClient(newClient);
clientList("Find all:", clientService.findAll());
System.out.println(client.getUserList());*/
}
private static void clientList (String message, List<Client> clientList) {
System.out.println(message);
for (Client client : clientList) {
System.out.println(client);
}
}
private static void userList (String message, List<User> userList) {
System.out.println(message);
for (User user : userList) {
System.out.println(user);
}
}
private static void troubleList (String message, List<Trouble> troubleList) {
System.out.println(message);
for (Trouble trouble : troubleList) {
System.out.println(trouble);
}
}
}
但是,当我使用MVC时,当父级更新时,所有子级都被从所有表中删除。
edit.jspx
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:spring="http://www.springframework.org/tags"
xmlns:form="http://www.springframework.org/tags/form"
xmlns="http://www.w3.org/1999/xhtml" version="2.0">
<jsp:directive.page contentType="text/html;charset=UTF-8"/>
<jsp:output omit-xml-declaration="yes"/>
<spring:message code="label_client_info" var="labelClientInfo"/>
<spring:message code="label_client_client" var="labelClient"/>
<spring:message code="label_client_id" var="labelId"/>
<spring:message code="label_client_inn" var="labelInn"/>
<spring:message code="label_client_specific" var="labelClientSpecific"/>
<spring:message code="label_client_address" var="labelClientAddress"/>
<spring:message code="label_client_phone" var="labelClientPhone"/>
<spring:message code="label_client_update" var="labelClientUpdate"/>
<spring:message code="label_client_new" var="labelClientNew"/>
<spring:eval expression="client.id == null ? labelClientNew:labelClientUpdate" var="formTitle"/>
<html>
<head><title>${formTitle}</title></head>
<body>
<h1>${formTitle}</h1>
<div id="contactUpdate">
<form:form modelAttribute="client" id="clientUpdateForm" method="post">
<c:if test="${not empty message}">
<div id="message" class="${message.type}">"${message.message}"</div>
</c:if>
<form:label path="client">
${labelClient}
</form:label>
<form:input path="client"/>
<div>
<form:errors path="client" cssClass="error"/>
</div>
<form:label path="inn">
${labelInn}
</form:label>
<form:input path="inn"/>
<div>
<form:errors path="inn" cssClass="error"/>
</div>
<form:label path="address">
${labelClientAddress}
</form:label>
<form:input path="address"/>
<div>
<form:errors path="address" cssClass="error"/>
</div>
<form:label path="phone">
${labelClientPhone}
</form:label>
<form:input path="phone"/>
<div>
<form:errors path="phone" cssClass="error"/>
</div>
<form:label path="clientSpecific">
${labelClientSpecific}
</form:label>
<form:select path="clientSpecific">
<form:options items="${clientSpecific}"/>
</form:select>
<form:hidden path="version"/>
<br/>
<button type="submit">Save</button>
<button type="reset">Reset</button>
</form:form>
</div>
</body>
</html>
</jsp:root>
ClientController
@RequestMapping("/clients")
@Controller
public class ClientController {
private Log log = LogFactory.getLog(ClientController.class);
private ClientService clientService;
private MessageSource messageSource;
@RequestMapping(method = RequestMethod.GET)
public String list (Model uiModel) {
log.info("Listing clients");
List<Client> clients = clientService.findAll();
uiModel.addAttribute("clients", clients);
log.info("No. of Clients: " + clients.size());
for (Client client : clients) {
System.out.println(client);
}
return "clients/list";
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String show(@PathVariable("id") Long id, Model uiModel){
Client client = clientService.findOne(id);
uiModel.addAttribute("client", client);
uiModel.addAttribute("users", client.getUserList());
return "clients/show";
}
@RequestMapping(value = "/{id}", params = "form", method = RequestMethod.POST)
public String update(Client client, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest, RedirectAttributes redirectAttributes, Locale locale){
log.info("Updating client:");
if(bindingResult.hasErrors()){
uiModel.addAttribute("message", new Message("error", messageSource.getMessage("client_save_fail", new Object[]{}, locale)));
uiModel.addAttribute("client", client);
return "clients/update";
}
uiModel.asMap().clear();
redirectAttributes.addFlashAttribute("message", new Message("success", messageSource.getMessage("client_save_success", new Object[]{}, locale)));
clientService.saveClient(client);
return "redirect:/clients/"+ UrlUtil.encodeUrlPathSegment(client.getId().toString(), httpServletRequest);
}
@RequestMapping(value = "/{id}", params = "form", method = RequestMethod.GET)
public String updateForm(@PathVariable("id") Long id, Model uiModel){
uiModel.addAttribute("client", clientService.findOne(id));
return "clients/update";
}
@RequestMapping(params = "form", method = RequestMethod.POST)
public String create(Client client, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest, RedirectAttributes redirectAttributes, Locale locale){
log.info("Creating Client:");
if(bindingResult.hasErrors()){
uiModel.addAttribute("message", new Message("error", messageSource.getMessage("client_create_fail", new Object[]{}, locale)));
uiModel.addAttribute("client", client);
}
uiModel.asMap().clear();
redirectAttributes.addFlashAttribute("message", new Message("success", messageSource.getMessage("client_create_success", new Object[]{}, locale)));
clientService.saveClient(client);
log.info("Client ID: " + client.getId() + "was created success");
return "redirect:/clients/" + UrlUtil.encodeUrlPathSegment(client.getId().toString(), httpServletRequest);
}
@RequestMapping(params = "form", method = RequestMethod.GET)
public String createForm(Model uiModel){
Client client = new Client();
uiModel.addAttribute(client);
return "clients/create";
}
@Autowired
public void setClientService(@Qualifier("jpaClientService") ClientService clientService){
this.clientService = clientService;
}
@Autowired
public void setMessageSource(MessageSource messageSource){
this.messageSource = messageSource;
}
}
我做错了什么? app-context == webapp-context
答案 0 :(得分:0)
解决。已删除&#34; orphanremoval = true&#34;来自实体。