java – JPA CriteriaBuilder使用@MapKeyColumn生成Map为空的查询

以下实体的参考情况:

碰撞 – > CollisionStatus< – > CollisionWorkgroup(加入实体) – >工作组

Collision.java

@Entity
@Table( name = "..." )
public class Collision
{
    ...

    @OneToOne( fetch = FetchType.EAGER, optional = false )
    @JoinColumn( name = "CSSTATE_ID", referencedColumnName = "CSSTATE_ID" )
    private CollisionStatus collisionStatus;

    ...
}

CollisionStatus.java :(感兴趣的类,见下文)

@Entity
@Table( name = "..." )
public class CollisionStatus
{
    ...

    // THIS IS THE MAPPING OF INTEREST:
    @OneToMany( mappedBy = "collisionStatus", fetch = FetchType.LAZY )
    @MapKeyColumn( name = "CLIENT_ID", insertable = false, updatable = false )
    private Map<Long, CollisionWorkgroup> collisionWorkgroups;

    ...
}

CollisionWorkgroup.java :(在CollisionStatus和Workgroup之间加入实体/表,PK = [CollisionStatusId,ClientId],都是Long)

@Entity
@Table( name = "..." )
public class CollisionWorkgroup
{
    @EmbeddedId
    protected CollisionWorkgroupEmbeddedPK pk;

    @MapsId( "collisionStatusId" )
    @JoinColumn( name = "CSSTATE_ID", referencedColumnName = "CSSTATE_ID" )
    private CollisionStatus     collisionStatus;

    @MapsId( "clientId" )
    @ManyToOne
    @JoinColumn( name = "CLIENT_ID", referencedColumnName = "CLIENT_ID" )
    private Client              client;

    @ManyToOne( fetch = FetchType.LAZY, optional = false )
    @JoinColumn( name = "WORKGROUP_ID", referencedColumnName = "WORKGROUP_ID" )
    private Workgroup           workgroup;

    ...
}

CollisionWorkgroupEmbeddedPK.java:(连接实体PK类)

@Embeddable
public class CollisionWorkgroupEmbeddedPK implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Column( name = "CSSTATE_ID" )
    private Long              collisionStatusId;

    @Column( name = "CLIENT_ID" )
    private Long              clientId;

    ...
}

Workgroup.java :(实际上不是很有趣,只是用来比较条件查询中的clientId)

@Entity
@Table( name = "..." )
public class Workgroup
{
    @Column( name = "CLIENT_ID", insertable = false, updatable = false )
    private Long              clientId;

    @ManyToOne( fetch = FetchType.EAGER, optional = false )
    @JoinColumn( name = "CLIENT_ID", referencedColumnName = "CLIENT_ID" )
    private Client            client;

    ...
}

其中有趣的映射是从CollisionStatus到连接实体CollisionWorkgroup,使用@MapKeyColumn连接工作组.

这意味着,每个客户端都可以将一个工作组与一个冲突的状态实体关联起来.登录到系统的每个用户只能看到来自其客户端的用户(每个用户属于一个客户端),但其他用户不会在UI上看到.

当按原样执行查询时,多个客户端设置工作组的任何冲突状态都会在我们的数据表/查询结果中产生其他结果.

这是有道理的,因此我需要向查询添加一些自定义谓词,以仅生成具有当前用户的客户端ID关联的条目.显示其他客户的条目是错误的.

我在SO上找到了这个:

Using JPA CriteriaBuilder to generate query where attribute is either in a list or is empty

我试过这段代码:

@Override
protected List<Predicate> createCustomPredicates( CriteriaBuilder builder, From<?, ?> root )
{
    List<Predicate> predicates = new ArrayList<>();

    Long clientId = this.sessionHelper.getCurrentClientId();
    Join<Collision, CollisionStatus> collisionStatus = root.join( "collisionStatus" );
    MapJoin<CollisionStatus, Long, CollisionWorkgroup> collisionWorkgroups = collisionStatus.<CollisionStatus, Long, CollisionWorkgroup>joinMap( "collisionWorkgroups", JoinType.LEFT );
    predicates.add( builder.and( builder.or( builder.isEmpty( collisionWorkgroups ),
                                             builder.equal( collisionWorkgroups.<String>get( "workgroup" ).<String>get( "clientId" ), clientId ) ) ) );

    ...
}

这给了我builder.isEmpty的编译错误(说:

Bound mismatch: The generic method isEmpty(Expression<C>) of type CriteriaBuilder
is not applicable for the arguments (MapJoin<CollisionStatus,Long,CollisionWorkgroup>).
The inferred type CollisionWorkgroup is not a valid substitute for the bounded
parameter <C extends Collection<?>>

显然,问题是Map不是Collection的子类.

问:

如何使用Criteria API在JPA中测试空Map?

最佳答案 是的,因为Map不是Collection.就Criteria API而言,JPA仅根据Collection定义IS_EMPTY谓词.由于这一点以及相关的CriteriaBuilder方法签名,您将无法通过JPA的CriteriaBuilder执行此操作.

真的JPA应该为该方法添加一个重载:

// existing method
<C extends Collection<?>> Predicate isEmpty(Expression<C> collection);
// overload
<C extends Map<?>> Predicate isEmpty(Expression<C> map);

但JPQL应该可行.

点赞