JpaModelTest.java 文件源码

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

项目:oma-riista-web 作者:
/**
 * In JPA, @ManyToOne and @OneToOne associations are fetched eagerly by
 * default. That will, in most situations, cause performance problems.
 * Because of that, the default behavior shall be changed to lazy fetching
 * which this test enforces. On some rare occasions, the developer may want
 * to apply eager fetching and for these cases @EagerFetch annotation is
 * available for indicating that the intention is purposeful and should be
 * passed by this test.
 */
@Test
public void nonInverseToOneAssociationsMustBeLazilyFetched() {
    final Stream<Field> failedFields = filterFieldsOfManagedJpaTypes(field -> {
        final OneToOne oneToOne = field.getAnnotation(OneToOne.class);
        final ManyToOne manyToOne = field.getAnnotation(ManyToOne.class);

        return !isIdField(field) && !field.isAnnotationPresent(EagerFetch.class) &&
                (manyToOne != null && manyToOne.fetch() == FetchType.EAGER ||
                        oneToOne != null && "".equals(oneToOne.mappedBy()) && oneToOne.fetch() == FetchType.EAGER);
    });

    assertNoFields(failedFields,
            "These entity fields should either have \"fetch = FetchType.LAZY\" parameter set on @ManyToOne/" +
                    "@OneToOne annotation or alternatively be annotated with @EagerFetch: ");
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号