我怎样才能在多选标准元组查询中加入列?

时间:2018-01-11 22:02:41

标签: java mysql hibernate-criteria many-to-one criteriaquery

表类:

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;

AND CRITERIA QUERY:

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"));

如果我删除连接和瞬态重写到列定义它可以工作,但我需要加入。 请帮忙

enter code here

1 个答案:

答案 0 :(得分:0)

不知道你想要实现什么,但最简单的解决方案

@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);