【问题标题】:Hibernate @OneToMany delete and inser againHibernate @OneToMany 删除并再次插入
【发布时间】:2014-10-17 07:59:52
【问题描述】:

我在使用 Hibernate 3.6.10.Final 时遇到问题。

我有一个使用以下方式映射的 OneToMany 关系:

public class Master implements Serializable {

    @Id
    @GeneratedValue(strategy = IDENTITY) 
    Long masterId;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "master")
    private List<Row> rows = new ArrayList<Attribute>(0);

    // Getter and setters and other properties

}

public class Row implements Serializable {
    @EmbeddedId
    @AttributeOverrides({
        @AttributeOverride(
            name = "masterId", 
            column = @Column(name = "masterId", nullable = false)),

        // Index is calculated 
        @AttributeOverride(
            name = "index", 
            column = @Column(name = "index", nullable = false, length = 18))
    })
    @NotNull
    private RowId id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "masterId", nullable = false, insertable = false, updatable = false)
    @XmlTransient
    private Master master;

    // Some other properties, getters and setters
}

我正在实现一个 saveAll REST 方法,从请求 json 序列化的对象是 @OneToMany 关系的完整表示。

我无法使用休眠自动行为,因为 Row.RowId.index 是使用旧算法计算的。

因此,为此,我在创建时执行此操作:

@Transactional
public void createAll(Master master) throws Exception {

    // Save in a variable the row list
    List<Row> rows = master.getRows();

    // Nullify the rows
    master.setRows(null);

    // Persists the Master using an helper
    masterService.persist(master);

    for (Row row : rows) {

        // the row helper method has the legacy index algorithm
        rowService.persist(row);

    }
}

这很好用,插入没有问题。

问题出现在更新上,当我需要在插入之前删除每一行时。 代码与插入完全相同,但调用 deleteAll 执行 HQL 更新查询。

@Transactional
public void updateAll(Master master) throws Exception {

    // Save in a variable the row list
    List<Row> rows = master.getRows();

    // Nullify the rows
    master.setRows(null);

    // Persists the Master using an helper
    masterService.merge(master);

    // Delete Everything before to save
    rowService.deleteAll(master);

    for (Row row : rows) {
        rowService.persist(row);
    }
}

这仅适用于在此更新之前没有行的情况。 当有行时,休眠删除所有内容,插入新行没有错误,但不会将它们持久化到数据库。

结果是清除所有当前主行。

有没有办法以明确的方式避免这个问题? 我不使用 SQL 来做什么我不想混合它们......

非常感谢,大卫。

【问题讨论】:

    标签: java sql hibernate


    【解决方案1】:

    @OneToMany 关系的级联类型设置为 any。这告诉 Hibernate 将更改同步到子节点。

    去掉cascadeType,一切正常。

    public class Master implements Serializable {
    
        @Id
        @GeneratedValue(strategy = IDENTITY) 
        Long masterId;
    
        @OneToMany(fetch = FetchType.LAZY, mappedBy = "master")
        private List<Row> rows = new ArrayList<Attribute>(0);
    
        // Getter and setters and other properties
    
    }
    

    非常感谢,大卫。

    【讨论】:

      猜你喜欢
      • 2016-04-22
      • 2013-06-28
      • 2014-11-16
      • 1970-01-01
      • 2015-02-18
      • 1970-01-01
      • 2016-05-16
      • 2012-09-16
      • 2011-03-19
      相关资源
      最近更新 更多