2015-10-26 44 views
0

我在表中有名爲eventUserRelationMapping的表中有兩個外鍵Event_id和Ringee_User_id。此eventUserRelationMapping在UserDO類下沒有單獨的DO類。這裏我正在嘗試爲前端使用獲取EventUserRelationMapping。如果我的方法,我得到了錯誤這樣如何修復無法添加或更新子行:外鍵約束失敗

Cannot add or update a child row: a foreign key constraint fails (`ringeeapp_dev`.`event_user_relation`, CONSTRAINT `FK_EVT_RINGEE_USER_ID` FOREIGN KEY (`RINGEE_USER_ID`) REFERENCES `ringee_user` (`RINGEE_USER_ID`)) 

但插入該eventUserRelationMApping表中的數據

這是DAOImpl我geteventUserMapping()方法

Override 
    public List<UserDO> getEventUserRelationMapping(UserDO userDO) 
      throws UserDataException { 
     JdbcTemplate jd = this.getJdbctemplate(); 
     int isNotDeleted = IRingeeConstants.IS_NOT_DELETED; 
     try 
     { 
      List<UserDO> userDOs = jd.query(GET_EVENT_USER_RELATION_MAPPING, new Object[] {userDO.getRingeeUserId() , isNotDeleted }, new RowMapper<UserDO>() { 
       @Override 
       public UserDO mapRow(ResultSet rs, int rowNum) throws SQLException { 
        UserDO userDO = new UserDO(); 
        userDO.setEventUserId(rs.getLong(1)); 
        userDO.setEventId(rs.getLong(2)); 
        userDO.setRingeeUserId(rs.getLong(3)); 
        userDO.setAttending(rs.getInt(4)); 
        userDO.setDeleted(rs.getInt(5)); 
        return userDO; 
       } 
     }); 
      return userDOs; 
    }catch (DataAccessException dExp) { 
     throw new UserDataException("Error while getting eventUserRelationMapping for user " + userDO.getRingeeUserId(), dExp); 
    } 
} 

這是查詢GET_EVENT_USER_RELATION_MAPPING

private static final String GET_EVENT_USER_RELATION_MAPPING = "SELECT EVENT_USER_ID, EVENT_ID, RINGEE_USER_ID, IS_ATTENDING, IS_DELETE FROM EVENT_USER_RELATION WHERE RINGEE_USER_ID = ? AND IS_DELETE = ? "; 

這是getEv的測試用例entUserRelationMapping

@Test 
    @Rollback(false) 
    public void testgetEventUserRelationMapping() { 
     ArrayList<UserDO> userDOs = new ArrayList<>(); 
     UserDO userDO = getUserDO(); 
     userDOs.add(userDO); 
     UserDO userDO1 = getUserDO1(); 
     userDOs.add(userDO1); 
     EventDO eventDO = getEventDO(); 
     eventDO.setRingeeUserId(userDO.getRingeeUserId()); 
     try { 
      eventDAOImpl.addEvent(eventDO); 
      userDAOImpl.addEventUserRelationMapping(userDOs, 
        eventDO.getEventId()); 
      List<UserDO> userDOs1 = userDAOImpl 
        .getEventUserRelationMapping(userDO); 
      Assert.assertEquals(1, userDOs1); 
     } catch (UserDataException uExp) { 
      uExp.printStackTrace(); 
      Assert.fail(); 
     } 

    } 

請幫我解決這個問題,爲什麼可以出現

這是MySQL查詢事件表

CREATE TABLE `event` (
    `EVENT_ID` BIGINT(20) NOT NULL, 
    `RINGEE_USER_ID` BIGINT(20) NOT NULL, 
    `TEXT` VARCHAR(45) NOT NULL, 
    `PLACE` VARCHAR(45) NOT NULL, 
    `EVENT_DATE` DATETIME NOT NULL, 
    `START_TIME` VARCHAR(10) NULL DEFAULT NULL, 
    `END_TIME` VARCHAR(10) NULL DEFAULT NULL, 
    `IS_DELETE` TINYINT(1) NULL DEFAULT '0', 
    `CREATED_DTTM` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP, 
    `MODIFIED_DTTM` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    PRIMARY KEY (`EVENT_ID`), 
    INDEX `EVENT_ID` (`EVENT_ID`), 
    INDEX `FK_EVENT_RINGEE_USER_ID` (`RINGEE_USER_ID`), 
    CONSTRAINT `FK_EVENT_RINGEE_USER_ID` FOREIGN KEY (`RINGEE_USER_ID`) REFERENCES `ringee_user` (`RINGEE_USER_ID`) ON UPDATE NO ACTION ON DELETE NO ACTION 
) 

THIS IS FOR EVENTUSERRELATION表MySQL查詢

CREATE TABLE `event_user_relation` (
    `EVENT_USER_ID` BIGINT(20) NOT NULL DEFAULT '0', 
    `EVENT_ID` BIGINT(20) NULL DEFAULT NULL, 
    `USER_RELATION_ID` BIGINT(20) NULL DEFAULT NULL, 
    `IS_ATTENDING` TINYINT(4) NULL DEFAULT NULL, 
    `CREATED_DTTM` TIMESTAMP NULL DEFAULT NULL, 
    `MODIFIED_DTTM` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    PRIMARY KEY (`EVENT_USER_ID`), 
    UNIQUE INDEX `EVENIT_ID_USER_RELATION_ID` (`EVENT_ID`, `USER_RELATION_ID`), 
    INDEX `FK_EVT_USR_USR_REL_ID` (`USER_RELATION_ID`), 
    CONSTRAINT `FK_EVT_USR_USR_REL_ID` FOREIGN KEY (`USER_RELATION_ID`) REFERENCES `user_relation` (`USER_RELATION_ID`), 
    CONSTRAINT `FK_EVT_USR_EVT_ID` FOREIGN KEY (`EVENIT_ID`) REFERENCES `event` (`EVENT_ID`) 
) 
+0

您能告訴我們兩個表的定義嗎? – Crazy2crack

+0

你需要查詢表格嗎? –

回答

0

首先我在你的@Test方法中看到一個錯誤

Assert.assertEquals(1,userDOs1); < ==> Assert.assertEquals(1,userDOs1.size());

爲了避免這種SQL的錯誤,你需要在你的表中插入數據和連接表中的 例: (TAB1,TAB2,tab_12) 值插入TAB1(提交) 值插入TAB2(提交) 將值插入到tab_12(提交)

+0

這些值已經添加在表中,但仍顯示這樣的錯誤 –

相關問題