spring-data-query-by-example结果不同于jpql结果

unhi4e5o  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(171)

在一次单元测试中,我在查询示例中发现了一个小的“奇怪行为”。
我有以下实体定义(keydrope实体模型)。
第二个结果列表是反向的,通常我不在嵌套属性上使用示例,但是因为它起作用了,所以它就很奇怪。示例列表与查询不同。我也提到这不是我的问题。。。我只是试着用一个例子来简化和db的谈话。。。

public class RoleEntity {
    @Id
    @Column(name="ID", length = 36)
    @Access(AccessType.PROPERTY) // we do this because relationships often fetch id, but not entity.  This avoids an extra SQL
    private String id;

    @Nationalized
    @Column(name = "NAME")
    private String name;
    @Nationalized
    @Column(name = "DESCRIPTION")
    private String description;

    // hax! couldn't get constraint to work properly
    @Column(name = "REALM_ID")
    private String realmId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "REALM")
    private RealmEntity realm;

    @Column(name="CLIENT_ROLE")
    private boolean clientRole;

    @Column(name="CLIENT")
    private String clientId;

    // Hack to ensure that either name+client or name+realm are unique. Needed due to MS-SQL as it don't allow multiple NULL values in the column, which is part of constraint
    @Column(name="CLIENT_REALM_CONSTRAINT", length = 36)
    private String clientRealmConstraint;

    @ManyToMany(fetch = FetchType.LAZY, cascade = {})
    @JoinTable(name = "COMPOSITE_ROLE", joinColumns = @JoinColumn(name = "COMPOSITE"), inverseJoinColumns =   @JoinColumn(name = "CHILD_ROLE"))
    private Set<RoleEntity> compositeRoles;

    @OneToMany(cascade = CascadeType.REMOVE, orphanRemoval = true, mappedBy="role")
    @Fetch(FetchMode.SELECT)
    @BatchSize(size = 20)
    protected Collection<RoleAttributeEntity> attributes;
}

 @Table(name = "ROLE_ATTRIBUTE")
@Entity
public class RoleAttributeEntity {

    @Id
    @Column(name = "ID", length = 36)
    @Access(AccessType.PROPERTY)
    protected String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ROLE_ID")
    protected RoleEntity role;

    @Column(name = "NAME")
    protected String name;

    @Nationalized
    @Column(name = "VALUE")
    protected String value;
}

@Repository
public interface RoleEntityRepository extends JpaRepository<RoleEntity, String> {

    @Query(value = "select r from RoleEntity r inner join r.attributes ra where ra.name = 'action'")
    List<RoleEntity> getSysmatActionRoles();

}

I executed this unit test as bellow. 

@Test
    void assertQueryResultRoleAttributeEntity() {
        RoleEntity roleEntitySample = new RoleEntity();
        RoleAttributeEntity rae = new RoleAttributeEntity();
        rae.setName("action");
        roleEntitySample.getAttributes().add(rae);
        Example<RoleEntity> exampleRole = Example.of(roleEntitySample);
        List<RoleEntity> lr1 = roleEntityRepository.findAll(exampleRole);
        List<RoleEntity> lr = roleEntityRepository.getSysmatActionRoles();
        assertNotNull(lr);
        assertTrue(lr.size() > 0);
        lr
        .stream()
        .forEach(r -> {
            System.err.println(r.getName());
        });
        //utializando example resultado incorreto.
        assertNotNull(lr1);
        assertTrue(lr1.size() > 0);
        lr1
        .stream()
        .forEach(r -> {
            System.err.println(r.getName());
        });

    }

只是扩展了单元测试来确定问题。spring数据示例不验证延迟集合(有问题的属性)。下面是第二个单元测试。用示例中的角色属性解决问题

@Test 
void assertQueryResultRoleAttributeEntity() {
    RoleAttributeEntity rae = new RoleAttributeEntity(); 
    rae.setName(SYSMAT_ACTION_NAME);
    Example<RoleAttributeEntity> ex = Example.of(rae);
    List<RoleAttributeEntity> list = roleAttributeEntityRepository.findAll(ex);
    list
    .stream()
    .forEach(r -> {
        System.err.println(r.getRole());
        //assertTrue(list1.size() == 1);
    });
}

Regards.

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题