1
votes
public class ClientEntity {

    @Id
    @Column(name="id", nullable = false, unique = true)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sqs_clients")
    @SequenceGenerator(name = "sqs_clients", sequenceName = "sqs_clients", allocationSize = 1)
    private Long id;

    @OneToMany(mappedBy = "client", cascade = CascadeType.ALL)
    Set<ParcelEntity> parcels;

    public void addParcel(ParcelEntity parcel) {
        if (parcel != null){
            if (parcels == null) {
                parcels = new HashSet<>();
            }
            parcels.add(parcel);
        }
    }
}


public class ParcelEntity {
    @Id
    @Column(name="id", nullable = false, unique = true)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sqs_parcels")
    @SequenceGenerator(name = "sqs_parcels", sequenceName = "sqs_parcels", allocationSize = 1)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "client_id", nullable = false)
    private ClientEntity client;

}
@Override
public void createOrder(Long clientId, CreateParcelReq createParcelReq) {
    clientRepo.findById(clientId).ifPresentOrElse(client-> {
        client.addParcel(parcelMapper.dtoToEntity(createParcelReq));
        clientRepo.save(client);
    }, ()->{throw new RuntimeException();});
}

I have created manyToOne relationship between parcel and client tables. Parcel entity is saved, but foreign key (clientId) is set to null in table. How can I fix it?