2015-09-02 31 views
1

本地查詢我需要寫使用JPA複雜查詢。我已經寫了下面的查詢:錯誤SQL中使用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 」。

有人可以找到正確的方式做到這一點幫助。

回答

0

您的別名聲明是錯誤的:你需要使用AS關鍵字。

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