检测到执行期间从行读取的主键被检测为空。主键不能包含null

时间:2016-11-30 15:32:27

标签: java mysql jpa

当我尝试在我的java应用程序中获取数据时,我使用EclipseLink for JPA。当我执行

 public Object doInJpa(EntityManager em) throws PersistenceException {
            Query q = em.createNativeQuery("SELECT t0.* FROM asset t0 WHERE t0.case_id = ?1  and t0.deleted=0 and t0.state='DEFAULT'  and fully_paid=0 and NOT EXISTS ( " +
                                                      " SELECT 1 from excluding_request er where er.asset_id=t0.ID and probable=0 and deleted = 0 and rejected = 0) order by t0.id limit ?2 , ?3", Asset.class);
            q.setParameter(1, caseId);
            q.setParameter(2, start);
            q.setParameter(3, limit);
            List<Asset> result = (List<Asset>) q.getResultList();
            return result;
        }
    }); 

我没有错误,但几乎相同的查询:

public Object doInJpa(EntityManager em) throws PersistenceException {
            Query q = em.createNativeQuery("SELECT COUNT(t0.ID) FROM asset t0 WHERE t0.case_id = ?1  and t0.deleted=0 and t0.state='DEFAULT'  and fully_paid=0 and NOT EXISTS ( " +
                                                      " SELECT 1 from excluding_request er where er.asset_id=t0.ID and probable=0 and deleted = 0 and rejected = 0)", Asset.class);
            q.setParameter(1, caseId);
            Integer result = ((List<Integer>)q.getSingleResult()).get(0);
            return result;
        }

我收到错误: 异常描述:从行读取的主键[DatabaseRecord(          =&GT; 37931)]在执行查询期间检测到为null。主键不得包含null。

看起来像count和eclipselink有些问题。顺便说一句,37931是我应该返回的结果。当我在MySQL中直接执行它们时,两个查询都能正常工作,无论我在count函数中放入什么错误都是相同的(*,1等等)

你能告诉我我做错了什么吗? 谢谢。

0 个答案:

没有答案