由于约束,即使使用提供的属性也无法保存对象

时间:2017-07-19 09:53:49

标签: java postgresql hibernate

我有一个对象Number,字段不可为空。在集成测试中,当我尝试保存对象时由于ConstraintViolationException而失败,这可能很奇怪,因为我提供了所有需要的字段。

我的测试详情

//other initializations needed for test  here

NumberGroup numberGroup = new NumberGroup();
    NumberGroupService.getDao().save(numberGroup);
    String numberGroupId = Long.toString(numberGroup.getId());

Number number = new Number("0987", true,numberGroup);
numberService.getDao().save(number);

//the rest of integration tests

我得到了以下有关stacktrace的详细信息:

org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; SQL [insert into number (field2, number_group_id, field1) values (?, ?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
...
...
Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into number (field2, number_group_id, field1) values ('1', 28, '0987') was aborted.  Call getNextException to see the cause.

以下是Number对象的详细信息。 @GenerateDao注释只是为了简化每个对象,只使用注释生成自己的dao。

@Data
@Entity
@GenerateDao
public class Number {

    @Id
    private String field1;

    private boolean field2;

    @ManyToOne(cascade = CascadeType.DETACH)
    @JoinColumn(name = "number_group_id", columnDefinition = "BIGINT")
    private NumberGroup numberGroup;

    public Number() {

    }

    public Number(String field1, boolean field2, NumberGroup numberGroup) {
        this.field1 = field1;
        this.field2 = field2;
        this.numberGroup = numberGroup;
    }
}

在stacktrace上,它还显示它具有所需的值。如果需要更多细节,请告诉我。

0 个答案:

没有答案