Cascading with filter

0

Good morning, I'm new to Hibernate and I have a doubt with cascading erasures.

I want to know if it is possible that the cascade deletion is made based on the value of a field, I explain.

I have the entities Country (id, name) and Province (id, name, country, enabled), with a relation 0 an, by which there could be provinces that having an associated country, said country is possible that it did not exist, in which case, the value of enabled for the Province entity would be "false".

The fact is that I want to know if it is possible to define the entity in some way that when the deletion of a country is made, just delete the provinces that having that country associated, have the value of enabled to "true". With what I know up to now, it eliminates all the provinces.

Entity Country:

@Entity
@Table(name = "Pais")
public class PaisEntity {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Integer id;

   @Column
   private Stringnombre;

   @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
   @JoinColumn(name = "pais")
   private List<ProvinciaEntity> listaProvincias; 

Province Entity

@Entity
@Table(name = "Provincia")
public class ProvinciaEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column
    private String nombre;

    @Column
    private Boolean habilitado;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "pais")
    private PaisEntity pais;

I do not know if I should change any annotation or add any attribute in the existing annotations.

Greetings and thanks for the help.

    
asked by Javi 10.08.2017 в 15:03
source

1 answer

1

No.

This comes from the referential integrity of the databases. Basically, if you have a field of one table referencing the ID of another table, and establishing the relationship for a foreign key, the value options for that field are:

  • An ID of the referenced table.

  • null , if that field is optional.

So, if you delete a record from the referenced table that has references, the options are:

  • Delete the records that reference it.

  • Set the reference to null .

  • Launch an error.

JPA reproduces that behavior (remember that below you can have the foreign key), there is no way to change it. You can not delete the country without deleting the province or at least putting the referral to null .

The obvious solution is, before deleting a country (how often are you going to have to erase a country? It is not that countries disappear every day 1 ), to go through all the provinces and, for those Interested, put your field of "country" to null so that they do not affect the deletion.

There is an annotation @PreRemove that allows you to mark a method to be executed before the removal of an entity. A @PreRemove in Pais is a good place to put that logic.

1 Even if it is only that you stop doing business with a country, a) the most normal thing is simply to disable the country so as not to lose information and b) it will not be very frequent.     
answered by 10.08.2017 в 15:44