У меня есть 2 таблицы, и я хочу обновить первую таблицу данными, после чего вторая таблица будет обновлена автоматически.Я новичок в весенней загрузке, и мне действительно нужна ваша помощь.
Я мог бы вставить данные из таблицы 1 в таблицу 2, но если я обновлю некоторые данные из таблицы 1, тогда таблица 2 не будет обновлена.
Что я могу сделать?
Это то, что я сделал до сих пор: две сущности таблиц и служба, с которой я работал, чтобы вставить данные в таблицу 2 из таблицы 1.
Таблица 1:
@Entity
@Table(name = "formation")
public class Formation {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String cursus;
private String groupeaction;
private String module;
private String formateur;
@Temporal(TemporalType.DATE)
private Date date;
private Long nbrappart;
private Long nbrabsent;
private Long hf;
private Long jf;
private Long nbrheures;
private Long tauxh;
private Long ristourneprevis;
private Long couthebergttc;
private Long coutpausecafttc;
Таблица 2:
@Entity
@Table(name = "tablef")
public class Tablef {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String cursus;
private Long nbrappart;
private Long Sumnbrheures;
private Long Sumjf;
private Long jhf;
private String groupeaction;
услуга, которую я использовал:
public Boolean InserIntoTableF(Tablef tf) {
Long id = ThreadLocalRandom.current().nextLong();
tf.setId(id);
jdbc.execute("insert into tablef (id,cursus,groupeaction
,nbrappart,sumnbrheures,sumjf,jhf)\r\n" +
"select id,cursus,groupeaction,nbrappart,sum(nbrheures),sum(jf)
,sum(jf)*nbrappart\r\n" +
" from formation \r\n" +
"group by cursus ;");
return true;
}
Контроллер:
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api")
@PreAuthorize("hasRole('ADMIN')")
public class FormationController {
@Autowired
private FormationService formationservice;
@Autowired
private FormationRepository formationrepository;
@GetMapping("/formations")
public List<Formation> GetAll() {
return formationrepository.findAll();
}
@GetMapping("/formations/{id}")
public ResponseEntity<Formation> getFormationById(@PathVariable(value = "id") Long formationId)
throws ResourceNotFoundException {
Formation formation = formationrepository.findById(formationId)
.orElseThrow(() -> new ResourceNotFoundException("Formation not found for this id :: " + formationId));
return ResponseEntity.ok().body(formation);
}
@PostMapping("/formations")
public Formation createFormation(@Valid @RequestBody Formation formation) {
return formationrepository.save(formation);
}
// this is how i update my entity formation (table 1)
@PutMapping("/formations/{id}")
public ResponseEntity<Formation> updateFormation(@PathVariable(value = "id") Long formationId,
@Valid @RequestBody Formation formationDetails) throws ResourceNotFoundException {
Formation formation = formationrepository.findById(formationId)
.orElseThrow(() -> new ResourceNotFoundException("Formation not found for this id :: " + formationId));
formation.setCursus(formationDetails.getCursus());
formation.setGroupeaction(formationDetails.getGroupeaction());
formation.setModule(formationDetails.getModule());
formation.setFormateur(formationDetails.getFormateur());
formation.setDate(formationDetails.getDate());
formation.setNbrappart(formationDetails.getNbrappart());
formation.setNbrabsent(formationDetails.getNbrabsent());
formation.setHf(formationDetails.getHf());
formation.setJf(formationDetails.getJf());
formation.setNbrheures(formationDetails.getNbrheures());
formation.setTauxh(formationDetails.getTauxh());
formation.setRistourneprevis(formationDetails.getRistourneprevis());
formation.setCouthebergttc(formationDetails.getCouthebergttc());
formation.setCoutpausecafttc(formationDetails.getCoutpausecafttc());
final Formation updatedFormation = formationrepository.save(formation);
return ResponseEntity.ok(updatedFormation);
}
@DeleteMapping("/formations/{id}")
public Map<String, Boolean> deleteFormation(@PathVariable(value = "id") Long formationId)
throws ResourceNotFoundException {
Formation formation = formationrepository.findById(formationId)
.orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + formationId));
formationrepository.delete(formation);
Map<String, Boolean> response = new HashMap<>();
response.put("deleted", Boolean.TRUE);
return response;
}
@PostMapping(value = "/fileupload")
public ResponseEntity<Formation> uploadFile(@ModelAttribute Formation formation) {
Boolean isFlag=formationservice.saveDataFromFile(formation.getFile());
if(isFlag) {
return new ResponseEntity<>(HttpStatus.OK);
}else
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
// here where i inser data from formation(table1) to tablef (table2)
@PostMapping(value = "/test")
public Boolean AddTf(Tablef tf) {
return formationservice.InserIntoTableF(tf);
}
}