Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

deletar relacionamento many to many, apenas relacionamento

não sei como fazer delete em uma tabela ManyToMany usando repository, apenas quero tirar a relação delas não apagar o registro

@Query(value = "delete from MotoristaParceiroMotorista m where m.idMotorista=: idMotorista " + " and m.idParceiroMotorista = :idParceiroMotorista ") ParceiroMotorista deletarVinculo(Long idMotorista, Long idParceiro);

@Entity @Getter @Setter @EqualsAndHashCode public class Motorista {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String nome;
private String cpf;
private String cnh;
private String telefone;
private Date dataDeNascimento;

@OneToMany(mappedBy = "motorista",fetch = FetchType.LAZY)
private List<Automovel> automovel;

@JsonIgnore
@OneToOne(fetch = FetchType.LAZY)
private ContaSalario contaSalario;

@JsonIgnore
@ManyToMany
private List<ParceiroMotorista> parceiroMotorista = new ArrayList<>();

@OneToMany(mappedBy = "motorista" ,fetch = FetchType.LAZY)
private List<Responsavel>responsavel = new ArrayList<>();


public Motorista(String nome, String cpf, String cnh, String telefone, Date dataDeNascimento) {

    this.nome = nome;
    this.cpf = cpf;
    this.cnh = cnh;
    this.telefone = telefone;
    this.dataDeNascimento = dataDeNascimento;

}

public Motorista() {
}

public Motorista(String nome, String telefone, Date dataDeNascimento, List<ParceiroMotorista> parceiroMotorista) {
    this.nome = nome;
    this.telefone = telefone;
    this.dataDeNascimento = dataDeNascimento;
    this.parceiroMotorista = parceiroMotorista;
}


public void adicionarAutomovel(Automovel automovel) {

    automovel.setMotorista(this);
    this.automovel.add(automovel);
}

public void adicionarResponsavel (Responsavel responsavel){

    responsavel.setMotorista(this);
    this.responsavel.add(responsavel);
}

public void adicionar(ParceiroMotorista parceiroMotorista) {
    List<Motorista> motorista = new ArrayList<>();

    parceiroMotorista.setMotorista(motorista);
    this.parceiroMotorista.add(parceiroMotorista); //adiciona na tabela do relacionamento many to many


}

public void remover(ParceiroMotorista parceiroMotorista) {


    this.parceiroMotorista.add(parceiroMotorista);
    parceiroMotorista.getMotorista().remove(this);


}

@Entity @Getter @Setter public class ParceiroMotorista {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String nome;
private String telefone;
private Date dataNascimento;
private String cpf;



@JsonIgnore
@ManyToMany (targetEntity = Motorista.class,mappedBy = "parceiroMotorista", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<Motorista> motorista = new ArrayList<>();


public ParceiroMotorista(String nome, String telefone, Date dataNascimento, String cpf) {
    this.nome = nome;
    this.telefone = telefone;
    this.dataNascimento = dataNascimento;
    this.cpf = cpf;
}

public ParceiroMotorista(String nome, String telefone) {
    this.nome = nome;
    this.telefone = telefone;
}

public ParceiroMotorista() {
}
1 resposta
solução!

resolvido, habilitei uma native query, para quem quiser no futuro:

@Query(value = "DELETE FROM vanescolar.motorista_parceiro_motorista WHERE motorista_id=? AND parceiro_motorista_id=?", nativeQuery = true) ParceiroMotorista deletarVinculo(Long idMotorista, Long idParceiro);

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software