У меня есть 2 объекта Client и Address с отношением один ко многим, как показано ниже, у объекта client есть репозиторий, в то время как у адресата нет, не могли бы вы сказать, каков наилучший способ обновить адрес, используя java 8, в бизнесе?слой?обе сущности используют аннотации lombok
@Entity
@Builder
@Getter
@Setter
@AllArgsConstructor
@RequiredArgsConstructor(staticName = "of")
@NoArgsConstructor
@ToString()
@EqualsAndHashCode()
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(columnDefinition="LONGTEXT")
private String commentaire;
@Embedded
private Contact contact
@OneToMany(cascade =
{CascadeType.PERSIST,CascadeType.REMOVE},orphanRemoval = true)
@Builder.Default
@OrderBy("id")
private Set<Adresse> adresses = new HashSet<>();
}
public class Adresse {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String cite;
@NonNull
private String pays;
private String region;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "client_id")
private Client client;
}
вот мой метод обновления клиента
@Service
@Transactional
public class ClientServiceImpl implements ClientService {
@Autowired
private ClientRepository clientRepository;
public Client update(Client newClient) {
return clientRepository.findById(newClient.getId()).map(client->{
client.setNom_Client(newClient.getNom_Client());
client.setDomaine_Client(newClient.getDomaine_Client());
client.setCommentaire(newClient.getCommentaire());
client.setContact(Contact.builder().nom(newClient.getContact().getNom()).prenom(newClient.getContact().getPrenom()).email(newClient.getContact().getEmail()).telephone(newClient.getContact().getTelephone()).build());
missing code client.getAdresses().addAll(newClient.getAdresses().stream().filter(a->a.getId()==null).collect(Collectors.toSet()));
}
return clientRepository.save(client);
}).orElseGet(()->{return clientRepository.save(newClient);});
}}
что ожидается: добавить новый адрес в существующий набор, если нетВ противном случае id_adresse обновляет поля с идентификатором в той же транзакции, так как я могу обновить адрес объекта?заранее спасибо