[英]Validate currency code matches one of the allowed values
I have a POJO, which contains a field called price
.我有一个 POJO,其中包含一个名为
price
的字段。
The field's type is javax.money.MonetaryAmount
, so that I can benefit from JSR 354 validation etc.该字段的类型是
javax.money.MonetaryAmount
,因此我可以从 JSR 354 验证等中受益。
I have a constraint on the field specifying that the currency must be EUR
:我对指定货币必须为
EUR
的字段有一个约束:
@Currency("EUR")
How can I specify that the currency must be either EUR
or USD
?如何指定货币必须是
EUR
或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;
}
}
The annotation supports String[]
as value
: https://github.com/hibernate/hibernate-validator/blob/master/engine/src/main/java/org/hibernate/validator/constraints/Currency.java注释支持
String[]
作为value
: https://github.com/hibernate/hibernate-validator/blob/master/engine/src/main/java/org/hibernate/validator/constraints/Currency.java
Though you can simply use @Currency({"EUR", "USD"})
虽然您可以简单地使用
@Currency({"EUR", "USD"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.