2

这是获取相关后端指定级别内的模块实体的方​​法。

public List<Module> getModulesWithinLevel(Long backendId, ModuleLevel... levels) {
        String joinedLevels = serializeLevels(levels);
        Query query = entityManager.createNativeQuery("SELECT DISTINCT M.* FROM MODULES M JOIN ROUTING_SEQUENCES RS ON RS.BCK_ID = M.MOD_ID WHERE M.MOD_LEVEL IN (?) AND RS.BCK_ID = ?", Module.class);
        query.setParameter(1, joinedLevels);
        query.setParameter(2, backendId);
        List l = query.getResultList();
        return l;
    }

当我在 sql developer 中执行确切的查询时,我得到一行结果。但是此方法返回空列表。日志中没有错误 - 只是空列表。我的本机查询有什么问题?

4

2 回答 2

0

你离开了吗?误入了那里?

尝试...

Query query = entityManager.createNativeQuery("SELECT DISTINCT M.* FROM MODULES M JOIN ROUTING_SEQUENCES RS ON RS.BCK_ID = M.MOD_ID WHERE M.MOD_LEVEL IN (:1) AND RS.BCK_ID = :2", Module.class);
于 2013-03-08T13:19:21.057 回答
0

我确实找到了原始问题的答案,但我通过使用复合键为 ROUTING_SEQUENCES 表实现实体做了一些解决方法。这很好用:

Query query = entityManager.createQuery("SELECT m FROM Module m WHERE m.id IN (SELECT rs.id.moduleId FROM RoutingSequence rs WHERE rs.id.backendId = :backendId) AND m.level IN (:levels)", Module.class);
query.setParameter("backendId", backendId);
query.setParameter("levels", Arrays.asList(levels));
return query.getResultList();

实体:

@Entity
@Table(name = "ROUTING_SEQUENCES")
public class RoutingSequence extends AbstractEntity<RoutingSequencePK> implements Auditable {

    @EmbeddedId
    private RoutingSequencePK id;

    @Column(name = "RS_MANDATORY", nullable = false)
    private Boolean mandatory;

    @Column(name = "RS_ORDER")
    private int order;

    @Column(name = "RS_INSDTTM")
    private Date insertDate;

    @Column(name = "RS_UPDDTTM")
    private Date updateDate;
...
}

ID

@Embeddable
public class RoutingSequencePK implements Serializable {

    @Column(name = "BCK_ID")
    private Long backendId;

    @Column(name = "MOD_ID")
    private Long moduleId;
于 2013-03-08T15:42:38.950 回答