使用Hibernate ScriptAssert错误的invalidValue

时间:2018-11-23 11:53:45

标签: java spring hibernate hibernate-validator

我创建了一个Spring Boot,DATA Rest,Hibernate应用程序。

我在自己的bean中添加了@ScriptAssert,它可以正常工作,但不幸的是,它不同于其他验证器,它返回一个对象作为无效值。

这是我的豆子:

public class DocumentRow extends AbstractEntity {
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Document document;

    @JsonDeserialize(using = ProductUriDeserializer.class)
    @Any(fetch = FetchType.EAGER, metaDef = "ProductDocumentRowDef", metaColumn = @Column(name = "productGroup"), optional = true)
    @AnyMetaDef(name = "ProductDocumentRowDef", metaType = "string", idType = "long", metaValues = {@MetaValue(value = "OL", targetEntity = OphthalmicLens.class),
            @MetaValue(value = "F", targetEntity = Frame.class), @MetaValue(value = "CL", targetEntity = ContactLens.class)})
    @JoinColumn(name = "product_id")
    private Product product;

    // The description of the product/note
    @Size(max = 255)
    @NotBlank
    @Column(nullable = false)
    private String description;

    // Reference to another document row (order/ddt/)

    @NotNull
    @Min(1)
    @Column(nullable = false)
    private int qty = 1;

    // taxable amount (without taxes)
    @NotNull //    @Min(value = 0)
    @Column(nullable = false, scale = 2)
    private BigDecimal unitPrice = BigDecimal.ZERO;

    // discount on taxable amount
    @NotNull
    @Min(value = 0)
    @Max(value = 99)
    @Column(nullable = false, scale = 2)
    private BigDecimal percentageDiscount = BigDecimal.ZERO;

    // (unit price * qty) - percentageDiscount% on that amount
    @NotNull
    @Min(value = 0)
    @Column(nullable = false, scale = 2)
    private BigDecimal amount = BigDecimal.ZERO;

    @JsonDeserialize(using = TaxRateUriDeserializer.class)
    @ManyToOne(fetch = FetchType.EAGER, optional = true)
    private TaxRate taxRate;

    // The total amount of taxes on taxable amount (unit price - percentageDiscount%) * qty)
    @NotNull
    @Min(value = 0)
    @Column(nullable = false, scale = 2)
    private BigDecimal taxAmount = BigDecimal.ZERO;

    // Total amount (amount+taxAmount)
    @Formula(value = "amount+taxAmount")
    private BigDecimal totalAmount;

    //says if the row is a note
    @NotNull
    @Column(nullable = false, columnDefinition = "BIT DEFAULT 0")
    private boolean note = false;

这是TaxRate:

public class TaxRate extends AbstractEntity {

    @NotBlank
    @Column(nullable = false)
    private String name;

    @NotBlank
    @Column(nullable = false, unique = true)
    private String code;

    @NotNull
    @Min(value = 0)
    @Max(value = 99)
    @Column(nullable = false, scale = 2)
    private BigDecimal rate;

    @Enumerated(EnumType.STRING)
    private TaxType type;

    @NotNull
    @Column(nullable = false, columnDefinition = "BIT DEFAULT 0")
    private boolean preset = false;

这是@ScriptAssert

@ScriptAssert(lang = "javascript", script = "_.note==false?_.taxRate!=null:true", alias = "_", reportOn = "taxRate", message = "{documentrow.taxrate.mandatory}")

这是我从休息控制器得到的响应:

{
  "errors" : [ {
    "entity" : "test.server.model.accounting.documents.DocumentRow",
    "property" : "taxRate",
    "invalidValue" : "test.server.model.accounting.documents.DocumentRow@95104f01",
    "message" : "{documentrow.taxrate.mandatory}"
  } ]
}

如何查看invalidValue是DocumentRow本身,而应该是TaxRate。 如何自定义从ScriptAssert返回的invalidValue?

0 个答案:

没有答案