Como Salva dados relacionados com spring boot | Fórum | School of Net

Deseja poder participar de nosso fórum e tirar todas as suas dúvidas?
Clique aqui e assine nosso plano de acesso ilimitado. Saiba mais.

por Laryssa

2 meses, 1 semana atrás Laryssa

Como Salva dados relacionados com spring boot

Bom tenho duas entidades a Person e Vehicle, suas class DTO e as class mapper que faz a comunicação entre dto e a entidade. Estou com dúvidas de como fazer para quando eu realizar o cadastro de vehicle que e vinculado ao Person. Meu Controller que deveria realizar essa tarefa(ao menos era o que eu pensava) ``` @RestController @RequestMapping(value = "/persons/{personId}/vehicle") public class VehicleController { @Autowired private PersonRepository personRepository; @Autowired private VehicleRepository vehicleRepository; private final VehicleMapper vehicleMapper = VehicleMapper.get(); @PostMapping @ResponseBody public ResponseEntity<String> create(@Valid @RequestBody VehicleDTO vehicleDTO, @PathVariable("personId") Long personId) { List<Person> person = (List<Person>) personRepository.findById(personId).orElse(null); System.out.println(person); Vehicle vehicleToSave = vehicleMapper.entity(vehicleDTO); vehicleToSave.setPersons(person); vehicleRepository.save(vehicleToSave); return new ResponseEntity<String>("OK" , HttpStatus.OK); } ``` Meu DTO - vehicle ``` public class VehicleDTO implements Serializable{ private static final long serialVersionUID = 1L; @NotNull(message = "The field 'brand' cannot be null") private String brand; @NotNull(message = "The field 'modelVehicle' cannot be null") private String modelVehicle; @NotNull(message = "The field 'year' cannot be null") @Past(message = "The field 'year' should be valid") @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy/MM") private LocalDate year; @NotNull(message = "The field 'tipy' cannot be null") private String type; } ``` Meu DTO - Person ``` public class PersonDTO implements Serializable{ private static final long serialVersionUID = 1L; @NotBlank(message = "The field 'cpf' cannot be empty/null") @CPF(message = "The field 'cpf' should be valid") @UniqueCpf private String cpf; @NotBlank(message = "The field 'name' cannot be empty/null") private String name; @NotBlank(message = "The field 'email' cannot be empty/null") @Email(message = "The field 'email' should be valid") @UniqueEmail private String email; @NotNull(message = "The field 'birthday' cannot be null") @Past(message = "The field 'birthday' should be valid") @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy") private LocalDate birthDay; private VehicleDTO vehicle; // Getters and setters ``` Minha entidade Person ``` public class Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; @Column(nullable = false) // Indica que o atributo é obrigatório private String name; @Column(unique = true, nullable = false, length = 11) private String cpf; @Column(unique = true, nullable = false) private String email; @Column(nullable = false) private LocalDate birthDay; @ManyToOne @JoinColumn(name = "vehicle_id") private Vehicle vehicle; ``` Minha Entidade Vehicle ``` public class Vehicle { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @OneToMany(mappedBy = "vehicle") //Endereço não é o lado dominante private List<Person> persons = new ArrayList<>(); @Column(nullable = false) private String brand; @Column(nullable = false) private String modelVehicle; @Column(nullable = false) private LocalDate year; @Column(nullable = false) private String type; ``` Mapper Vehicle ``` public class VehicleMapper { public static VehicleMapper get() { return new VehicleMapper(); } public Vehicle entity(VehicleDTO vehicleDTO) { Vehicle vehicle = new Vehicle(); vehicle.setBrand(vehicleDTO.getBrand()); vehicle.setModelVehicle(vehicleDTO.getModelVehicle()); vehicle.setType(vehicleDTO.getType()); vehicle.setYear(vehicleDTO.getYear()); return vehicle; } public VehicleDTO dto(Vehicle vehicle) { VehicleDTO dto = new VehicleDTO(); dto.setBrand(vehicle.getBrand()); dto.setModelVehicle(vehicle.getModelVehicle()); dto.setType(vehicle.getType()); dto.setYear(vehicle.getYear()); return dto; } } ```

2 Respostas