简体   繁体   中英

Can't save more than 2 Elements in ArrayList to MySQL with SpringBoot?

I can't save more than 2 Elements in ArrayList to MySQL with SpringBoot. When i make a post request (Postman or with Alamofire) with one object in array i get status code 200 otherwise I get status code 500 and this error message in springboot console:

java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails ( quran-pro-core . surah , CONSTRAINT FKibuxjpjiwpq46220g7sn7y1q FOREIGN KEY ( id ) REFERENCES reciter ( documentid ))

These are my classes:

@RestController
public class ReciterController {

     @Autowired
     private ReciterService reciterService;

     @PostMapping("/reciters")
     public String saveReciter(@RequestBody Reciter reciter) {
     reciterService.saveReciter(reciter);
     return "SUCCESS";
   }
}

@Service
public class ReciterService {

     @Autowired
     private ReciterRepostitory reciterRepository;

     public void saveReciter(Reciter reciter) {
     reciterRepository.save(reciter);
     }
}

@Entity
public class Reciter {

     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Long documentID;

     @Column
     private String name;
     @Column
     private String image;
     @OneToMany(mappedBy="id", cascade = CascadeType.ALL)
     private List<Surah> surahs;

     // Constructor and getters and setters
}

@Entity
public class Surah {

     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Long id;

     @Column
     private String name;
     @Column
     private int number;

     // Constructor and getters and setters

}

This body works:

{
"name": "reciter1",
"image": "/reciter1/image.png",
"surahs": [
         {
            "name": "LOLll",
            "number": 0
        }

]
}

and this doesn't:

{
"name": "reciter1",
"image": "/reciter1/image.png",
"surahs": [
         {
            "name": "LOLll",
            "number": 0
        },
        {
            "name": "test2",
            "number": 1
        }
]
}

enter code here

You ManyToOne configuration is not correct, the mappedBy element must be used to specify the relationship field or property of the entity that is the owner of the relationship

  1. First suggestion, use bidirectional relation :
public class Reciter {

     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Long documentID;

     @Column
     private String name;
     @Column
     private String image;
     @OneToMany(mappedBy="reciter", cascade = CascadeType.ALL)
     private List<Surah> surahs;

     // Constructor and getters and setters

}
@Entity
public class Surah {

     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Long id;

     @Column
     private String name;
     @Column
     private int number;

     @ManyToOne
    private Reciter reciter;
}
  1. or use Unidirectional relation :
 @OneToMany(cascade = CascadeType.ALL)
 private List<Surah> surahs;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM