JPA entities are not saved correctly

4

I have the following entities, all 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 father and that the children are automatically created), the method is the following:

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 you save, you create the first child correctly, SiteFloor, but the rest, SiteRow and SiteChair do not create them. Do not waterfalls with so many nested elements work?

    
asked by Raider 22.12.2015 в 19:39
source

1 answer

3

Within the cycle for(SiteFloor siteFloor : siteTemplate.getFloors()){ you create a SiteFloor new in each iteration, but I do not see by any side that you have sf.setRows(var);

It seems that the problem is that when you reach the second cycle you find an empty collection that you created in POJOS

    @OrderBy("orden")
    @Where(clause = "deleteDate is null")
    @OneToMany(mappedBy = "floor", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<SiteRow> rows = new HashSet<SiteRow>();

And nothing is iterated

Tip, use

 SiteFloor sf = null;
 for(SiteFloor siteFloor : siteTemplate.getFloors()){
        sf = new SiteFloor();

instead of

 for(SiteFloor siteFloor : siteTemplate.getFloors()){
        SiteFloor sf = new SiteFloor();
    
answered by 22.12.2015 / 20:16
source