表类:

private static final long serialVersionUID = 1L;
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
@JoinColumn(name = "SUBSYSTEM_ID", referencedColumnName = "ID")
@ManyToOne
private Subsystem subsystem;

@Transient
private Long subsystemId;


和标准查询:

public PagedSearchResult<Tuple> findByUserId1(Long id, UserSubsystemSearchCriteria searchCriteria) {
        final CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<Tuple> query = criteriaBuilder.createTupleQuery();
        final Root<UserSubsystem> from = query.from(UserSubsystem.class);
        query.multiselect(from.get("subsystemId"));
        query.groupBy(from.get("subsystemId"));


如果我删除了连接并暂时重写为列定义,则可以使用,但是我需要该连接。

最佳答案

不知道您要实现的目标,但是最简单的解决方案

@Entity
class UserSubsystem {

    private static final long serialVersionUID = 1L;

    @Id
    private Long id;

    @JoinColumn(name = "SUBSYSTEM_ID", referencedColumnName = "ID")
    @ManyToOne
    private Subsystem subsystem;

    @Column(name = "SUBSYSTEM_ID", updatable = false, insertable = false)
    private Long subsystemId;

}


和查询

CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<UserSubsystem> query = criteriaBuilder.createQuery(UserSubsystem.class);
Root<UserSubsystem> from = query.from(UserSubsystem.class);
from.fetch("subsystem", JoinType.LEFT);
query.groupBy(from.get("subsystemId"));
entityManager.createQuery(query);

关于java - 如何访问多选条件元组查询中的联接列?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48216529/

10-12 04:02
查看更多