Ainda não tem acesso? Estude com a gente! Matricule-se
Ainda não tem acesso? Estude com a gente! Matricule-se

Como apagar registro da tabela associativa

Pessoal, alguem pode me ajudar com um CRUD do meu projeto? Estou desenvolvendo uma aplicação com Spring MVC e Hibernate. Consigo criar as tabelas, inserir, ler e alterar registros, mas quando faço um delete, a tabela associativa não tem o registro deletado. A aplicação tem a temática de Pets, um usuário possui varios Pets, cada pet pertence somente a esse usuário, utilizei a anotação OneToMany para o Set dentro da classe usuário. Eis o código:

JPAConfiguration:

package br.com.phirsonpets.config;

import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
public class JPAConfiguration {

    @Bean
    public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean() {
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();

        JpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        factoryBean.setJpaVendorAdapter(adapter);

        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setUsername("root");
        dataSource.setPassword("");
        dataSource.setUrl("jdbc:mysql://localhost:3306/phirsonpets");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");

        factoryBean.setDataSource(dataSource);

        Properties properties = new Properties();

        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        properties.setProperty("hibernate.show_sql", "true");
        properties.setProperty("hibernate.hbm2ddl.auto", "update");

        factoryBean.setJpaProperties(properties);
        factoryBean.setPackagesToScan("br.com.phirsonpets.model");

        return factoryBean;
    }

    @Bean
    public JpaTransactionManager transactionManager() {
        return new JpaTransactionManager();
    }

}

Usuario

package br.com.phirsonpets.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

@Entity
@Component
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class Usuario implements UserDetails {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "usuario_id")
    private String id;
    private String email;
    @OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.REMOVE }, orphanRemoval = true)
    @JoinTable(joinColumns = @JoinColumn(name = "usuario_id"), inverseJoinColumns = @JoinColumn(name = "pet_id"))
    private Set<Pet> pets = new HashSet<>();
    @ManyToMany(fetch = FetchType.EAGER)
    private List<Role> roles = new ArrayList<Role>();
    private String nome;
    private Genero genero;
    private String cpf;
    private String celular;
    private boolean cuidador;
    private String cep;
    private String rua;
    private String bairro;
    private String uf;
    private String numero;
    private String cidade;
    private String senha;
    private double notaMedia;
    private int petIndice;

//Getters e setters
}

Pet

package br.com.phirsonpets.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Pet implements Serializable {


    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "pet_id")
    private String id;
    private int indice;
    private String nome;
    private TipoAnimal tipoAnimal;
    private String raca;
    private GeneroAnimal genero;
    private double notaMedia;
//Getters e Setters
    }