hibernate 级联保存 8条数据 子表有的时候保存4条 有的时候保存3条

alicas 发布于 2015/07/23 22:27
阅读 187
收藏 0
hibernate 级联保存 8条数据 本地保存可以保存8条,服务器保存有的时候保存4条,有的时候保存8条


主表


@Entity
@Table(name = "salas_log")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class SalasLog extends BaseObject implements Serializable {
    private static final long serialVersionUID = 4651023848122222715L;
    private Integer id;
    private Date addtime;
    private Date updatetime;
    private String twm;
    private String nwm;
    private String experience;
    private String type;
    private Boolean isComments;
    private User user;
    private String comments;
    private Integer praise;
    private String terminal;
    private Set<SalasFile> salasFiles = new HashSet<SalasFile>();
    private Set<SalasFocus> salasFocuses = new HashSet<SalasFocus>();
    private Set<SalasComments> salasCommentses = new HashSet<SalasComments>();
    private Set<SalasPraise> salasPraises = new HashSet<SalasPraise>();
    private Set<SalasReply> salasReplies = new HashSet<SalasReply>();
    private Set<SalasAcl> salasAcls = new HashSet<SalasAcl>();
    private Set<SalasRead> salasReads = new HashSet<SalasRead>();




    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Integer getId() {
        return id;
    }




    public void setId(Integer id) {
        this.id = id;
    }




    @Basic
    @Column(name = "ADDTIME")
    public Date getAddtime() {
        return addtime;
    }




    public void setAddtime(Date addtime) {
        this.addtime = addtime;
    }




    @Basic
    @Column(name = "UPDATETIME")
    public Date getUpdatetime() {
        return updatetime;
    }




    public void setUpdatetime(Date updatetime) {
        this.updatetime = updatetime;
    }




    @Basic
    @Column(name = "TWM")
    public String getTwm() {
        return twm;
    }




    public void setTwm(String twm) {
        this.twm = twm;
    }




    @Basic
    @Column(name = "NWM")
    public String getNwm() {
        return nwm;
    }




    public void setNwm(String nwm) {
        this.nwm = nwm;
    }




    @Basic
    @Column(name = "EXPERIENCE")
    public String getExperience() {
        return experience;
    }




    public void setExperience(String experience) {
        this.experience = experience;
    }




    @Basic
    @Column(name = "TYPE")
    public String getType() {
        return type;
    }




    public void setType(String type) {
        this.type = type;
    }




    @Basic
    @Column(name = "ISCOMMENTS")
    public Boolean getIsComments() {
        return isComments;
    }




    public void setIsComments(Boolean isComments) {
        this.isComments = isComments;
    }




    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "USERID")
    public User getUser() {
        return user;
    }




    public void setUser(User user) {
        this.user = user;
    }




    @Basic
    @Column(name = "COMMENTS")
    public String getComments() {
        return comments;
    }




    public void setComments(String comments) {
        this.comments = comments;
    }




    @Basic
    @Column(name = "PRAISE")
    public Integer getPraise() {
        return praise;
    }




    public void setPraise(Integer praise) {
        this.praise = praise;
    }




    @Basic
    @Column(name = "TERMINAL")
    public String getTerminal() {
        return terminal;
    }




    public void setTerminal(String terminal) {
        this.terminal = terminal;
    }




    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "salasLog")
    @JsonIgnore
    public Set<SalasFile> getSalasFiles() {
        return salasFiles;
    }




    public void setSalasFiles(Set<SalasFile> salasFiles) {
        this.salasFiles = salasFiles;
    }




    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "salasLog")
    @JsonIgnore
    public Set<SalasFocus> getSalasFocuses() {
        return salasFocuses;
    }




    public void setSalasFocuses(Set<SalasFocus> salasFocuses) {
        this.salasFocuses = salasFocuses;
    }




    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "salasLog")
    @JsonIgnore
    public Set<SalasComments> getSalasCommentses() {
        return salasCommentses;
    }




    public void setSalasCommentses(Set<SalasComments> salasCommentses) {
        this.salasCommentses = salasCommentses;
    }




    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "salasLog")
    @JsonIgnore
    public Set<SalasPraise> getSalasPraises() {
        return salasPraises;
    }




    public void setSalasPraises(Set<SalasPraise> salasPraises) {
        this.salasPraises = salasPraises;
    }




    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "salasLog")
    @JsonIgnore
    public Set<SalasReply> getSalasReplies() {
        return salasReplies;
    }




    public void setSalasReplies(Set<SalasReply> salasReplies) {
        this.salasReplies = salasReplies;
    }




    @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY, mappedBy = "salasLog",orphanRemoval=true)
    @JsonIgnore
    public Set<SalasAcl> getSalasAcls() {
        return salasAcls;
    }




    public void setSalasAcls(Set<SalasAcl> salasAcls) {
        this.salasAcls = salasAcls;
    }




    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "salasLog",orphanRemoval=true)
    @JsonIgnore
    public Set<SalasRead> getSalasReads() {
        return salasReads;
    }




    public void setSalasReads(Set<SalasRead> salasReads) {
        this.salasReads = salasReads;
    }




    public void addSalasAcl(SalasAcl salasAcl){
        getSalasAcls().add(salasAcl);
    }








    public void addSalasFile(SalasFile salasFile){
        getSalasFiles().add(salasFile);
    }




    public void addSalasRead(SalasRead salasRead){
        getSalasReads().add(salasRead);
    }



子表:


@Entity
@Table(name = "salas_acl")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class SalasAcl extends BaseObject implements Serializable {
    private static final long serialVersionUID = 3361846524157425229L;
    private Integer id;
    private Date addtime;
    private User user;
    private SalasLog salasLog;




    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Integer getId() {
        return id;
    }




    public void setId(Integer id) {
        this.id = id;
    }




    @Basic
    @Column(name = "ADDTIME")
    public Date getAddtime() {
        return addtime;
    }




    public void setAddtime(Date addtime) {
        this.addtime = addtime;
    }




    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "USERID")
    public User getUser() {
        return user;
    }




    public void setUser(User user) {
        this.user = user;
    }




    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "SALASLOGID")
    public SalasLog getSalasLog() {
        return salasLog;
    }




    public void setSalasLog(SalasLog salasLog) {
        this.salasLog = salasLog;
    }





action:


public String save() throws Exception {
        salasLog.setAddtime(new Date());
        salasLog.setUpdatetime(new Date());
        salasLog.setIsComments(false);
        salasLog.setPraise(0);
        salasLog.setUser(getSessionUser());
        salasLog.setTerminal("pc");
        String tempAcl;
        if (getRequest().getParameterValues("acl") != null) {
            tempAcl = StringUtil.arrToString(getRequest().getParameterValues("acl")) + "," + getUserAccessManager.getZongbuUser() + "," +
                    getUserAccessManager.getThisZongjingliUser(getSessionUser().getDepartments()) + "," +
                    getUserAccessManager.getThisGsglyUser(getSessionUser().getDepartments());
        } else {
            tempAcl = getUserAccessManager.getZongbuUser() + "," +
                    getUserAccessManager.getThisZongjingliUser(getSessionUser().getDepartments()) + "," +
                    getUserAccessManager.getThisGsglyUser(getSessionUser().getDepartments());
        }
        String[] aclArr = tempAcl.split(",");
        Set<String> aclSet = new HashSet<String>(Arrays.asList(aclArr));
        salasLog.getSalasAcls().clear();
        salasLog.getSalasReads().clear();
        for (String acl : aclSet) {
            SalasAcl salasAcl = new SalasAcl();
            salasAcl.setAddtime(new Date());
            salasAcl.setUser(userManager.getUser(acl));
            salasAcl.setSalasLog(salasLog);
            salasLog.addSalasAcl(salasAcl);
            SalasRead salasRead = new SalasRead();
            salasRead.setIsread(false);
            salasRead.setAddtime(new Date());
            salasRead.setUser(userManager.getUser(acl));
            salasRead.setSalasLog(salasLog);
            salasLog.addSalasRead(salasRead);
        }
        String path = "upload/salasFile/salasLog/" + RandomUtil.getRandomString(15) + "/";
        if (picture != null) {
            //上传图片
            ImageUtil.uploadImageByAll(path, pictureFileName, picture);
            SalasFile salasFilep = new SalasFile();
            salasFilep.setAddtime(new Date());
            salasFilep.setFilename(pictureFileName);
            salasFilep.setFileurl(path);
            salasFilep.setType(1);
            salasFilep.setSalasLog(salasLog);
            salasLog.addSalasFile(salasFilep);
        }
        if (file != null) {
            //上传文件
            FileUtil.uploadFile(path, fileFileName, file);
            SalasFile salasFilef = new SalasFile();
            salasFilef.setAddtime(new Date());
            salasFilef.setFilename(fileFileName);
            salasFilef.setFileurl(path);
            salasFilef.setType(2);
            salasFilef.setSalasLog(salasLog);
            salasLog.addSalasFile(salasFilef);
        }
        salasLogManager.save(salasLog);
        saveMessage("添加成功");
        return SUCCESS;
    }






加载中
返回顶部
顶部