JPA本机查询不返回任何记录

时间:2013-03-08 13:11:50

标签: oracle hibernate jpa nativequery

这是让模块在相关后端的指定级别内进行访问的方法。

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中执行完全查询时,我得到一行结果。 但是这个方法返回空列表。日志中没有错误 - 只是空列表。 我的原生查询有什么问题?

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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;
...
}

编号

@Embeddable
public class RoutingSequencePK implements Serializable {

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

    @Column(name = "MOD_ID")
    private Long moduleId;