spring boot error 4070无法添加或更新子行

时间:2016-03-14 12:15:21

标签: java mysql spring jpa

我花了很多时间尝试破解这个很长时间但是没有用。我有一个数据库,我想从某个表user_test_summary获取一些数据并将这些数据插入另一个表follow_forum.so far it工作,但我一直从春季启动日志中得到这个错误

 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`))

我需要解答如何删除这些错误,我还包括任务文件,存储库和我加入exam_id的java文件

任务文件       package 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);
 }
}

存储库

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);

}

java文件

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;
 }

0 个答案:

没有答案