I have two tables. Site
have a lot of Sector
s.
When I try to edit the list of sectors of a site, for example I have 3 sectors initially, so I delete one sector and I update the site. in the database are three sectors of the Site.
Is it possible that the sectors are automatically deleted when I update a site? or I have to compare one to one?
Thanks.
Site entity
public class Site { @Id @Column(name = "ID") @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; .... @OneToMany(fetch = FetchType.EAGER, mappedBy = "site", cascade = CascadeType.ALL, orphanRemoval = true) private List<Sector> sectores; }
Sector entity
public class Sector { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idGenerator") @SequenceGenerator(name="idGenerator", sequenceName="SECTOR_SEQ") @Column(name = "IDSECT") private Integer id; @Column(name = "NOMBRE") private String nombre; @ManyToOne @JoinColumn(name = "IDSITE") private Site site; }
log
08:17:55,096 INFO [stdout] (http--0.0.0.0-8080-2) Hibernate: update SITE set SITIO_CODIGO=?, SITIO_NOMBRE=? where ID=? 08:17:55,116 INFO [stdout] (http--0.0.0.0-8080-2) Hibernate: update SECTOR set NOMBRE=?, IDSITIO=? where IDSECT=?
Advertisement
Answer
I don’t know if you got it yet, but maybe the @GeneratedValue is a problem for it is different from the Sites’ @GeneratedValue
but if that is not the case then maybe change
@OneToMany(fetch = FetchType.EAGER, mappedBy = "site", cascade = CascadeType.ALL
To
@OneToMany(cascade= {CascadeType.ALL }, mappedBy="mcrPosTran") @LazyCollection(LazyCollectionOption.FALSE)
And change in the sector
@ManyToOne @JoinColumn(name = "IDSITE") private Site site;
To
@ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name = "IDSITE") private Site site;