0

我有一个 POJO,其中包含一个名为price.

该字段的类型是javax.money.MonetaryAmount,因此我可以从 JSR 354 验证等中受益。

我对该字段有一个约束,指定货币必须是EUR

@Currency("EUR")

如何指定货币必须是EURor USD

import org.hibernate.annotations.Columns;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.validator.constraints.Currency;
import org.jadira.usertype.moneyandcurrency.moneta.PersistentMoneyAmountAndCurrency;

import javax.money.MonetaryAmount;
import javax.persistence.*;

@Entity
@TypeDef(name = "persistentMoneyAmountAndCurrency", typeClass = PersistentMoneyAmountAndCurrency.class)
public class LocalizedProduct {

    @Id
    @GeneratedValue
    private long id;

    @Currency("EUR")
    // @Currency("EUR, USD") .. doesn't work
    @Columns(columns = {
            @Column(name = "amount_currency", length = 3),
            @Column(name = "amount_value", precision = 19, scale = 5)})
    @Type(type = "persistentMoneyAmountAndCurrency")
    private MonetaryAmount price;

    public long getId() {
        return this.id;
    }

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

    public MonetaryAmount getPrice() {
        return this.price;
    }

    public void setPrice(MonetaryAmount price) {
        this.price = price;
    }
}

4

1 回答 1

6

注释支持String[]valuehttps ://github.com/hibernate/hibernate-validator/blob/master/engine/src/main/java/org/hibernate/validator/constraints/Currency.java

虽然你可以简单地使用@Currency({"EUR", "USD"})

于 2021-03-01T12:42:06.627 回答