使用jpa进行本机查询的SQL出错

时间:2015-09-02 20:45:40

标签: java sql jpa

我需要使用JPA编写复杂的查询。我写了以下问题:

select temp2.role_id role_id, temp2.contact_id contact_id, temp2.account_id  account_id, temp2.username username, temp2.first_name first_name,  temp2.last_name  last_name, temp.email email  from (SELECT  role_id FROM    portal_role_privs START WITH granted_role_id in (select role_id from portal_role where name = :name) CONNECT BY granted_role_id = PRIOR role_id) temp3 inner join (select role_id, temp.contact_id contact_id, temp.account_id  account_id, temp.username username, temp.first_name first_name,  temp.last_name last_name, temp.email email  from portal_user_grp_privs pugp inner join ( select * from portal_user_data where account_id = :accountId ) temp on temp.contact_id = pugp.contact_id) temp2 on temp3.role_id = temp2.role_id.

和代码:

EntityManager entityManager = EntityManagerUtil.getInstance().currentAsapEntityManager();
        logger.debug(LOGGER_PREFIX + "Obtained Entity Manager");
        Query query = entityManager.createQuery(NATIVE_GET_ADMIN_CONTACTS);
        query.setParameter("accountId", accountId);
        query.setParameter("name", "admin");
        List<Object[]> accManagers = query.getResultList();
        List<AccountManagers> accountManagers = new ArrayList<>();
        for (Object[] obj : accManagers) {
            AccountManagers accountMgr = new AccountManagers();
            accountMgr.setAccountId((String) obj[2]);
            accountMgr.setRoleType("admin");
            accountMgr.setContactId((String) obj[1]);
            accountMgr.setLoginName((String) obj[3]);
            accountMgr.setFirstName((String) obj[4]);
            accountMgr.setLastName((String) obj[5]);
            accountMgr.setEmail((String) obj[6]);
            accountManagers.add(accountMgr);

        }
        return accountManagers;

但这会引发错误“第1:22行:意外令牌:role_id:第1:22行:意外令牌:role_id”。

有人可以帮助找到正确的方法。

1 个答案:

答案 0 :(得分:0)

您的别名声明为false:您需要使用AS关键字。

select temp2.role_id as role_id, temp2.contact_id as contact_id ...