JPAAnnotationsConfigurer.java 文件源码

java
阅读 34 收藏 0 点赞 0 评论 0

项目:oval 作者:
protected void initializeChecks(final Column annotation, final Collection<Check> checks, final AccessibleObject fieldOrMethod) {
    /* If the value is generated (annotated with @GeneratedValue) it is allowed to be null
     * before the entity has been persisted, same is true in case of optimistic locking
     * when a field is annotated with @Version.
     * Therefore and because of the fact that there is no generic way to determine if an entity
     * has been persisted already, a not-null check will not be performed for such fields.
     */
    if (!annotation.nullable() && !fieldOrMethod.isAnnotationPresent(GeneratedValue.class) && !fieldOrMethod.isAnnotationPresent(Version.class)
            && !fieldOrMethod.isAnnotationPresent(NotNull.class))
        if (!containsCheckOfType(checks, NotNullCheck.class))
            checks.add(new NotNullCheck());

    // add Length check based on Column.length parameter, but only:
    if (!fieldOrMethod.isAnnotationPresent(Lob.class) && // if @Lob is not present
            !fieldOrMethod.isAnnotationPresent(Enumerated.class) && // if @Enumerated is not present
            !fieldOrMethod.isAnnotationPresent(Length.class) // if an explicit @Length constraint is not present
    ) {
        final LengthCheck lengthCheck = new LengthCheck();
        lengthCheck.setMax(annotation.length());
        checks.add(lengthCheck);
    }

    // add Range check based on Column.precision/scale parameters, but only:
    if (!fieldOrMethod.isAnnotationPresent(Range.class) // if an explicit @Range is not present
            && annotation.precision() > 0 // if precision is > 0
            && Number.class.isAssignableFrom(fieldOrMethod instanceof Field ? ((Field) fieldOrMethod).getType() : ((Method) fieldOrMethod).getReturnType()) // if numeric field type
    ) {
        /* precision = 6, scale = 2  => -9999.99<=x<=9999.99
         * precision = 4, scale = 1  =>   -999.9<=x<=999.9
         */
        final RangeCheck rangeCheck = new RangeCheck();
        rangeCheck.setMax(Math.pow(10, annotation.precision() - annotation.scale()) - Math.pow(0.1, annotation.scale()));
        rangeCheck.setMin(-1 * rangeCheck.getMax());
        checks.add(rangeCheck);
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号