0

I have two tables. Site have a lot of Sectors.

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=?
JohnPortella
  • 1,791
  • 5
  • 21
  • 30
  • as per my understanding, it should remove the entry from the sector table once you update the site object. just make sure that the list present inside the site object has now only 2 elements instead of 3. – Code2Interface Aug 06 '14 at 13:26
  • The list contains two elements, then I update the site. When I go back to check, the 3 elements appear again. – JohnPortella Aug 06 '14 at 13:29
  • mappedBy = "sitio" might be an issue...i think it should be mappedBy = "site" – Code2Interface Aug 06 '14 at 13:31
  • It is a problem of translation, and I corrected. When I change some value of the sectors are updated, but when I remove an item from the list and then update the site, is still listed as his child. – JohnPortella Aug 06 '14 at 13:34
  • FetchType.EAGER is sometimes an issue as also encountered here https://forum.hibernate.org/viewtopic.php?f=9&t=1004627 – Code2Interface Aug 06 '14 at 13:41
  • I changed for Site: @OneToMany(cascade = {CascadeType.REMOVE}, mappedBy="site", fetch = FetchType.LAZY) and for Sector @JoinColumn(name = "IDSITIO") @ManyToOne(optional = true) @ForeignKey(name = "IDSITIO") but the problem is the same – JohnPortella Aug 06 '14 at 13:51

1 Answers1

0

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;
Rika
  • 768
  • 1
  • 5
  • 16