2016-06-13 15 views
0

私は、エンティティ・データ・フィールドに範囲の制約を実装する必要があります。ビーン検証範囲の制約

@Entity 
@Table(name = "T_PAYMENT") 
public class Payment extends AbstractEntity { 

    //.... 

    //Something like that 
    @Range(minValue = 80, maxValue = 85) 
    private Long paymentType; 

} 

私はすでに、検証サービスを作成したが、これらの例の多くを実装する必要があります。

挿入された番号が範囲外の場合、例外をスローする必要があります。

+1

をサポート。'@Max(85)\t @Min(5)'この検証用のカスタムメッセージを定義する場合は、カスタム制約を作成する必要があります。この[リンク]を参照してください(http://www.thejavageek.com/2014/05/24/jpa-constraints/) – ypp

+0

http://stackoverflow.com/questions/5129825/difference-between-max-and-decimalmax-最小および最小10分 –

答えて

2

依存関係の下に追加pom.xmlで範囲チェック

@Min(value = 80) 
@Max(value = 85) 
private Long paymentType; 

を定義することができますhibernate-validator

Please refer to this official documentation.

注釈

@Range(min=, max=) 

では、次の注釈を使用して範囲を定義することができるデータの種類

BigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types 

使用

Checks whether the annotated value lies between (inclusive) the specified minimum and maximum 
1

hibernate-validator依存関係を使用すると、あなたが必要とする

<dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-validator</artifactId> 
     <version>{hibernate.version}</version> 
    </dependency>