Java spring启动错误4070无法添加或更新子行

Java spring启动错误4070无法添加或更新子行,java,mysql,spring,jpa,Java,Mysql,Spring,Jpa,我花了很长时间试图破解这个问题,但没有成功。我有一个数据库,我想从某个表用户\u test\u summary获取一些数据,并将这些数据插入另一个表中。到目前为止,它可以工作,但我一直从spring引导日志中得到这些错误 2016-03-14 10:44:30.778 ERROR 4070 --- [ main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000388: Unsuccessful: alter table u

我花了很长时间试图破解这个问题,但没有成功。我有一个数据库,我想从某个表用户\u test\u summary获取一些数据,并将这些数据插入另一个表中。到目前为止,它可以工作,但我一直从spring引导日志中得到这些错误

 2016-03-14 10:44:30.778 ERROR 4070 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000388: Unsuccessful: alter table user_test_summary add constraint FK_jwbmc8sr6h9l1484f7j0ounhm foreign key (exam_id) references exam (id)
2016-03-14 10:44:30.781 ERROR 4070 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : Cannot add or update a child row: a foreign key constraint fails (`jambDb`.`#sql-448_2f`, CONSTRAINT `FK_jwbmc8sr6h9l1484f7j0ounhm` FOREIGN KEY (`exam_id`) REFERENCES `exam` (`id`))
请告诉我如何删除这些错误的答案,我还包括了任务文件、存储库和我加入考试id的java文件

任务文件 包com.axumtechnologies.cron.task.getusersfromfollowedforum

 import java.text.SimpleDateFormat;
 import java.util.List;
 import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.axumtechnologies.cron.LoginUser;
import com.axumtechnologies.cron.TaskConfiguration;
import com.axumtechnologies.cron.task.getusersfromfollowedforum.FollowedForumRep;


@Component
public class followedForumTask {

 private static Logger logger = LoggerFactory.getLogger(TaskConfiguration.class);
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");


@Autowired
private TaskConfiguration taskConfiguration; 

@Autowired
private UserTestSummaryRepository userTestSummaryRepository;

@Autowired
private FollowedForumRep FollowedForumUsersRepository;
@Value("${followed_forum_user_per_run}")
public Integer totalUsersPerRun;


  @Scheduled(cron="${followed_forum_task_cron_expression}") //run after 2 minutes
public void ReportfollowedForum(){

    reportUserTestSummary();
  }


  public void reportUserTestSummary(){
System.out.println("**********************************STARTING one-time task to make people follow subjects for tests they've already taken************************");
      System.out.println("**************************************************************************************************************************************");
    System.out.println("Run at : "+ (new Date().toString()));
    //store in  list in a variable
List <UserTestSummary> users = userTestSummaryRepository.findUsersWhoTookExams(new PageRequest(0,totalUsersPerRun));

 //loop through list
for(UserTestSummary t : users){

   //code to check if the user is already following the subject
  List<FollowedForum> followers = FollowedForumUsersRepository.findByid(t.getUser(), t.getExam().getSubject().getForumid());

   //if the user is not following the subject make him follow it else do nothing
   if(followers.isEmpty()){
         FollowedForum insertIntoFollowedForum = new FollowedForum();
         insertIntoFollowedForum.setUser(t.getUser());
         insertIntoFollowedForum.setForumId(t.getExam().getSubject().getForumid());
         FollowedForumUsersRepository.save(insertIntoFollowedForum);
         System.out.println("this user is not following "+ t.getExam().getSubject().getForumid());
       }else
      {
           System.out.println("this user is now following "+ t.getExam().getSubject().getForumid());       
       }

    }
   }

  public List<FollowedForum> getAllFollowedUsers(LoginUser user, Integer forumid)  {
    return FollowedForumUsersRepository.findByid(user , forumid);
 }
}

如果你能把问题的范围缩小到一些特定的行,明确地问一个关于某件事的问题,你会得到更好的回答。如指南中所述,并创建一个。似乎您正试图在user\u test\u summary.exam\u id->exam.id上添加外键约束。我的假设是,您的考试表中有一个没有参考id的考试id。你检查过了吗?如果你能把问题缩小到特定的范围,明确地问一个关于某件事的问题,你会得到更好的回答。如指南中所述,并创建一个。似乎您正试图在user\u test\u summary.exam\u id->exam.id上添加外键约束。我的假设是,您的考试表中有一个没有参考id的考试id。你查过了吗?
package com.axumtechnologies.cron.task.getusersfromfollowedforum;

import org.springframework.data.domain.*;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.*;

import java.util.List;

 public interface UserTestSummaryRepository extends  Repository<UserTestSummary, Long> {

Page<UserTestSummary> findAll(Pageable pageable);


 //See link for more details -
 //http://docs.spring.io/spring-data/jpa/docs/1.5.x/reference/html/jpa.repositories.html#jpa.query-methods
//query
 @Query("select t from UserTestSummary t where t.exam is not null and t.exam.id is not null and t.exam.subject is not null and t.exam.subject.forumid is not null")
 List<UserTestSummary> findUsersWhoTookExams(Pageable pageable);

}
package com.axumtechnologies.cron.task.getusersfromfollowedforum;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;


import com.axumtechnologies.cron.LoginUser;


@Entity()
public class UserTestSummary{

@Id  
@GeneratedValue 
private Long id;

//join user_id column
@ManyToOne
@JoinColumn(name = "user_id")
private LoginUser user;



//join exam_id column
@ManyToOne
@JoinColumn(name = "exam_id")
private Exam exam;


 //generated getters and setters
 public Long getId() {
    return id;
 }


 public void setId(Long id) {
    this.id = id;
  }


 public Exam getExam() {
    return exam;
  }


 public void setExam(Exam exam) {
    this.exam = exam;
 }


 public LoginUser getUser() {
    return user;
 }


 public void setUser(LoginUser user) {
    this.user = user;
 }