JpaModelTest.java 文件源码

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

项目:oma-riista-web 作者:
@Test
public void nullableConstraintsMustBeMutuallyConsistent() {
    final Stream<Field> failedFields = filterFieldsOfManagedJpaTypes(field -> {
        if (field.isAnnotationPresent(Version.class) || field.isAnnotationPresent(ElementCollection.class)) {
            return false;
        }

        final boolean isPrimitive = field.getType().isPrimitive();

        final boolean notNull = field.isAnnotationPresent(NotNull.class);
        final boolean notEmpty = field.isAnnotationPresent(NotEmpty.class);
        final boolean notBlank = field.isAnnotationPresent(NotBlank.class);

        final boolean notNullOrEmpty = notNull || notEmpty || notBlank;

        final boolean embedded = field.isAnnotationPresent(Embedded.class);

        final Column column = field.getAnnotation(Column.class);
        final ManyToOne manyToOne = field.getAnnotation(ManyToOne.class);
        final OneToOne oneToOne = field.getAnnotation(OneToOne.class);
        final JoinColumn joinColumn = field.getAnnotation(JoinColumn.class);

        if (column != null) {
            if (manyToOne != null && column.nullable() != manyToOne.optional() ||
                    oneToOne != null && column.nullable() != oneToOne.optional() ||
                    joinColumn != null && column.nullable() != joinColumn.nullable() ||
                    column.nullable() && (notNullOrEmpty || isPrimitive) ||
                    !isPrimitive && column.insertable() && !column.nullable() && !notNullOrEmpty) {

                return true;
            }
        }
        if (joinColumn != null) {
            if (manyToOne != null && joinColumn.nullable() != manyToOne.optional() ||
                    oneToOne != null && joinColumn.nullable() != oneToOne.optional() ||
                    joinColumn.nullable() && notNull ||
                    joinColumn.insertable() && !joinColumn.nullable() && !notNull) {

                return true;
            }
        }
        if (manyToOne != null) {
            if (!isIdField(field) && manyToOne.optional() == notNull) {
                return true;
            }
        }
        if (oneToOne != null) {
            if (!isIdField(field) && oneToOne.optional() == notNull) {
                return true;
            }
        }
        if (notNullOrEmpty && !embedded) {
            if (column == null && joinColumn == null && manyToOne == null && oneToOne == null) {
                return true;
            }
        }

        return false;
    });

    assertNoFields(failedFields,
            "Entity fields should have consistency with regard to:\n" +
                    "    (1) Presence of @NotNull, @NotBlank or @NotEmpty\n" +
                    "    (2) Values of @Column.nullable, @ManyToOne.optional, @OneToOne.optional and " +
                    "@JoinColumn.nullable\n" +
                    "    (3) Whether the type of field is primitive or not\n" +
                    "  These fields fail: ");
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号