合并不会在JPA中更新

时间:2014-11-17 11:14:01

标签: java spring-mvc jpa spring-data-jpa

拥有此实体类:

@Entity
@Table(name="t_period")
public class Period {

    @Id
    @GeneratedValue(strategy = SEQUENCE, generator = "periodSeq")
    @Column(name = "PERIOD_ID", unique = true, nullable = true)
    @SequenceGenerator(name = "periodSeq", sequenceName = "seq_period", allocationSize=1)
    private Long periodId;

    @OneToOne(fetch = FetchType.EAGER, cascade=CascadeType.ALL) 
    @JoinColumn(name = "CLASS_GROUP_ID", referencedColumnName = "ID")
    ClassGroup classGroup;

    @Column(name="PERIOD1")
    Integer period1;

    @Column(name="PERIOD2")
    Integer period2;

    ...
}

和..

/**
 * ProductGroup generated by Tomasz Borowiec
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "T_CLASS_GROUP", uniqueConstraints = @UniqueConstraint(columnNames = "KEY"))
@SequenceGenerator(name = "seqCLASS_GROUP", sequenceName = "SEQ_CLASS_GROUP")
public class ClassGroup implements java.io.Serializable {

    private Long id;
    private String key;


    @Id
    @Column(name = "ID", unique = true, nullable = false, precision = 38, scale = 0)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqCLASS_GROUP")
    public Long getId() {
        return this.id;
    }

...

}

DAO

public class PeriodDaoImpl extends JpaDaoSupport implements PeriodDao {
       ...
    @Override
    public Period saveOrUpdate(Period period) throws Exception {

        if (period.getPeriodId() == null &&
                find(period.getClassGroup()) == null) {
            getEntityManager().persist(period);

        } else {

            period = getEntityManager().merge(period);
        }

        return period;
    }
        ...
}

这个代码在Junit中测试DAO:

    ClassGroup classGroup = classGroupDao.findById(1);

    Period period = new Period();
    period.setClassGroup(classGroup)
    period.setPeriod1(1);

    period = periodDao.saveOrUpdate(period);
    period.setPeriod1(2);
    period = periodDao.saveOrUpdate(period);

我在第二次更新中遇到此错误,执行合并,而不是持久

在DataBase级别,我为对象classGroup设置了一个唯一的Key:

CREATE TABLE t_period
(
  period_id number(38) not null,
  class_group_id number(38) not null,
  period1 number,
  period2 number,
  CONSTRAINT pk_period PRIMARY KEY        (period_id),
  CONSTRAINT fk_class_group   FOREIGN KEY (class_group_id) REFERENCES t_class_group(id),
  CONSTRAINT uk_class_group UNIQUE        (class_group_id)
);

ORA-00001:违反了唯一约束(ENV_ECAT.UK_CLASS_GROUP)

1 个答案:

答案 0 :(得分:1)

使用此代码,persist永远不会执行,因为find(period.getClassGroup())永远不会返回null。这意味着merge()被调用了两次,但它没有给你回复ID,所以我认为这就是为什么它试图两次插入同一个对象。

因此,请尝试从find(...)语句中删除if,看它是否有效。

if (period.getPeriodId() == null) {
    getEntityManager().persist(period);
}

如果确实有效,您必须提出不同的条件来分支到merge()persist()的来电。