我在该表中有一个名为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 = ? ";
这是getEventUserRelationMapping的测试用例
@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
)
这是EVENTUSERRELATION TABLE MYSQL QUERY
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 :(得分:0)
首先,我在@Test方法中看到错误
Assert.assertEquals(1,userDOs1); &LT; ==&GT; Assert.assertEquals(1,userDOs1.size());
要避免此SQL错误,您需要在表中和连接表中插入数据 例如: (tab1,tab2,tab_12) 将值插入tab1(提交) 将值插入tab2(提交) 将值插入tab_12(提交)