2016-11-30 541 views
0

我想保存一個註釋,我使用的是來自客戶表的外鍵(c_id),但它說外鍵約束不能添加或更新數據庫「無法添加或更新子行:外鍵約束失敗」

"Cannot add or update a child row: a foreign key constraint fails 
(`db_hotel`.`tbl_comments`, CONSTRAINT `tbl_comments_ibfk_1` FOREIGN KEY (`c_id`)" 

我的實體類

public class Comments { 
    private int commentsId; 
    private Customer customer; 
    private String message; 

    public Comments() { 
    } 

    public Comments(int commentsId, Customer customer, String message) { 
     this.commentsId = commentsId; 
     this.customer = customer; 
     this.message = message; 
    } 

    public int getCommentsId() { 
     return commentsId; 
    } 

    public void setCommentsId(int commentsId) { 
     this.commentsId = commentsId; 
    } 

    public Customer getCustomer() { 
     return customer; 
    } 

    public void setCustomer(Customer customer) { 
     this.customer = customer; 
    } 

    public String getMessage() { 
     return message; 
    } 

    public void setMessage(String message) { 
     this.message = message; 
    } 

    @Override 
    public String toString() { 
     return "Comments{" + "commentsId=" + commentsId + ", customer=" + customer + ", message=" + message + '}'; 
    } 


} 

我實現部分爲CRUD操作

@Repository(value = "CommentsDAO") 
public class CommentsDAOImpl implements CommentsDAO { 

    @Autowired 
    private JdbcTemplate jdbcTemplate; 

    @Override 
    public List<Comments> getALL() throws SQLException { 
     return jdbcTemplate.query(SQLConstant.COMMENTS_GETALL, new RowMapper<Comments>() { 

      @Override 
      public Comments mapRow(ResultSet rs, int i) throws SQLException { 
       return mapData(rs); 
      } 
     }); 
    } 

    @Override 
    public int insert(Comments comm) throws SQLException { 
     return jdbcTemplate.update(SQLConstant.COMMENTS_INSERT, new Object[]{comm.getCommentsId(), comm.getCustomer().getC_id(), comm.getMessage()}); 
    } 

    private Comments mapData(ResultSet rs) throws SQLException { 
     Comments comments = new Comments(); 
     comments.setCommentsId(rs.getInt("comments_id")); 
     Customer customer = new Customer(); 
     customer.setC_id(rs.getInt("c_id")); 
     customer.setFirstName(rs.getString("first_name")); 
     customer.setLastName(rs.getString("last_name")); 
     customer.setEmail(rs.getString("email")); 
     customer.setContactNo(rs.getInt("contact_no")); 
     comments.setCustomer(customer); 
     comments.setMessage(rs.getString("message")); 
     return comments; 
    } 

    @Override 
    public int update(Comments comm) throws SQLException { 
     return jdbcTemplate.update(SQLConstant.COMMENTS_UPDATE, new Object[]{comm.getMessage(), comm.getCustomer().getC_id(), comm.getCommentsId()}); 
    } 

    @Override 
    public int delete(int commentsId) throws SQLException { 
     return jdbcTemplate.update(SQLConstant.CUSTOMER_DELETE, new Object[]{commentsId}); 
    } 

    @Override 
    public Comments getById(int commentsId) throws SQLException { 
     return (Comments) jdbcTemplate.query(SQLConstant.COMMENTS_GETBYID, new Object[]{commentsId}, new ResultSetExtractor<Comments>() { 

      @Override 
      public Comments extractData(ResultSet rs) throws SQLException, DataAccessException { 
       Comments comments = null; 
       if (rs.next()) { 
        comments = new Comments(); 
        comments.setCommentsId(rs.getInt("comments_id")); 
        Customer customer = new Customer(); 
        customer.setC_id(rs.getInt("c_id")); 
        customer.setFirstName(rs.getString("first_name")); 
        customer.setLastName(rs.getString("last_name")); 
        customer.setEmail(rs.getString("email")); 
        customer.setContactNo(rs.getInt("contact_no")); 
        comments.setCustomer(customer); 
        comments.setMessage(rs.getString("message")); 

       } 
       return comments; 
      } 
     }); 
    } 

} 

控制器部分

@Controller 
@RequestMapping(value="/admin/comments") 
public class CommentsController { 
    @Autowired 
    private CommentsService commentsService; 
    @Autowired 
    private CustomerService customerService; 


    @RequestMapping(method = RequestMethod.GET) 
    public String index (ModelMap map) throws SQLException{ 
     map.addAttribute("Comments",commentsService.getALL()); 
     return "admin/comments/index"; 
    } 
    @RequestMapping(value="/add",method=RequestMethod.GET) 
    public ModelAndView add(HttpServletRequest request) throws SQLException { 
     ModelAndView mv= new ModelAndView("/admin/comments/add"); 
     String c_id=null; 
     if (request.getParameter("c_id")!=null && !request.getParameter("c_id").equals("")) { 
       c_id=request.getParameter("c_id"); 
     } 

     mv.addObject("Customer", customerService.getALL()); 
     return mv; 
    } 

    @RequestMapping(value = "/delete/{commentsId}",method = RequestMethod.GET) 
    public String delete(@PathVariable("commentsId") int commentsId)throws SQLException { 
     commentsService.delete(commentsId); 
     return "redirect:/admin/comments"; 
    } 

    @RequestMapping(value="/save",method=RequestMethod.POST) 
    public String save(@ModelAttribute ("Comments")Comments comment, @ModelAttribute("Customer")Customer customer) throws SQLException { 
     try { 
      comment.setCustomer(customer); 
      if(comment.getCommentsId()==0) { 
       commentsService.insert(comment); 
      } else { 
       commentsService.update(comment); 
      } 
     } catch (SQLException ex) { 

     } 
     return "redirect:/admin/comments"; 
    } 
} 

這裏是JSP插入或ADD

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 
<h1>Add Users</h1> 
<form:form modelAttribute="Comments" action="${SITE_URL}/admin/comments/save" method="post" role="form"> 

<div class="form-group"> 
      <label for="c_id">CustomerId</label> 

      <select path="c_id" class="form-control"> 
       <option value="0">Select Commentor</spring:option> 
       <c:forEach var="customer" items="${Customer}"> 
        <option value="${customer.getC_id()}">${customer.getFirstName()} 
        </option> 
       </c:forEach> 
      </select> 
     </div>  


<div class="form-group"> 
     <label>Message</label> 
     <form:input path ="message" placeholder="Enter Message" required="required" class="form-control"/> 
    </div> 

    <form:hidden path="commentsId"/> 
    <div class="form-group"> 
    <button type="submit" class="btn btn-success" >Save</button> 
    </div> 
</form:form> 

我想solvde它由於兩個天,但無法弄清楚..

+0

外部約束錯誤意味着在Customer表中找不到引用的Customer(by c_id) - 但這也可能意味着您傳遞了null值。您是否已檢查並確認該客戶存在 - 並且您確實通過了客戶ID? – ishmaelMakitla

+0

客戶表存在,當我嘗試直接從MySQL插入它的作品,但是當我嘗試從jsp插入它給了我上述錯誤。並在選擇選項我從客戶表中獲取客戶的所有名稱 –

回答

0

在對錶格執行任何操作之前,請禁用外鍵檢查。只需查詢 SET FOREIGN_KEY_CHECKS = 0 這將禁用與任何其他表的外鍵匹配。然後再次測試。 完成表後,再次啓用查詢 SET FOREIGN_KEY_CHECKS = 1

+0

設置外鍵檢查= 0後給了我同樣的錯誤.stuck在同一件事情,因爲兩天... –

+0

我想我無法通過保存時保存c_id –

相關問題