I have the following entities, all of them related as follows:
@Entity
@Table(name = "site")
public class Site extends BaseEntity {
/**
* Serial
*/
private static final long serialVersionUID = -6249200015501840399L;
private String name;
private Boolean template;
@OrderBy("id")
@Where(clause = "deleteDate is null")
@OneToMany(mappedBy = "site", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<Ceremony> ceremonies = new HashSet<Ceremony>();
@OrderBy("orden")
@Where(clause = "deleteDate is null")
@OneToMany(mappedBy = "site", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<SiteFloor> floors = new HashSet<SiteFloor>();
}
@Entity
@Table(name = "site_floor")
public class SiteFloor extends BaseEntity {
/**
* Serial
*/
private static final long serialVersionUID = -7377030638076319589L;
private String name;
private Integer orden;
@Lob
@Column(length=65000)
private String map;
private Boolean template;
@ManyToOne
private Site site;
@OrderBy("orden")
@Where(clause = "deleteDate is null")
@OneToMany(mappedBy = "floor", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<SiteRow> rows = new HashSet<SiteRow>();
}
@Entity
@Table(name = "site_row")
public class SiteRow extends BaseEntity {
/**
* Serial
*/
private static final long serialVersionUID = -197546085868764752L;
private String name;
private Integer orden;
private Boolean template;
@ManyToOne
private SiteFloor floor;
@OrderBy("orden")
@Where(clause = "deleteDate is null")
@OneToMany(mappedBy = "row", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<SiteChair> chairs = new HashSet<SiteChair>();
}
@Entity
@Table(name = "site_chair")
public class SiteChair extends BaseEntity {
/**
* Serial
*/
private static final long serialVersionUID = -5188420274496228482L;
private String name;
private Integer orden;
private Boolean temp;
private Boolean blocked;
private Boolean template;
@ManyToOne
private Entidad entity;
@ManyToOne
private SiteRow row;
}
And I have a method that creates all the children and my intention is to save them all in cascade (in other words save the parent and automatically create the children), the method is as follows:
private Site nuevoSitio(Site siteTemplate, SiteService siteServ){
Site newSite = new Site();
newSite.setId(null);
newSite.setTemplate(false);
newSite.setName(siteTemplate.getName());
for(SiteFloor siteFloor : siteTemplate.getFloors()){
SiteFloor sf = new SiteFloor();
sf.setId(null);
sf.setTemplate(false);
sf.setName(siteFloor.getName());
sf.setOrden(siteFloor.getOrden());
sf.setMap(siteFloor.getMap());
sf.setSite(newSite);
for(SiteRow siteRow : sf.getRows()){
SiteRow sr = new SiteRow();
sr = siteRow;
sr.setId(null);
sr.setTemplate(false);
sr.setOrden(siteRow.getOrden());
sr.setFloor(sf);
for(SiteChair siteChair : sr.getChairs()){
SiteChair sc = new SiteChair();
sc.setId(null);
sc.setTemplate(false);
sc.setOrden(siteChair.getOrden());
sc.setTemp(siteChair.getTemp());
sc.setBlocked(siteChair.getBlocked());
sc.setEntity(null);
sc.setRow(sr);
sr.getChairs().add(sc);
}
sf.getRows().add(sr);
}
newSite.getFloors().add(sf);
}
return siteServ.saveOrUpdate(newSite);
}
The problem is that when saving, it correctly creates the first child, SiteFloor, but the rest, SiteRow and SiteChair, it does not create. Don't cascades work with so many nested elements?
Inside the cycle
for(SiteFloor siteFloor : siteTemplate.getFloors()){
you create aSiteFloor
new one in each iteration, but I don't see anywhere that you havesf.setRows(var);
It seems that the problem is that when it reaches the second cycle it finds an empty collection that you created in the POJOS
And nothing is iterated
Dude, use
instead of