JPA 2 No explicit selection and an implicit one cold not be determined

Farouk Alhassan picture Farouk Alhassan · Apr 18, 2011 · Viewed 15.7k times · Source

I am trying to fetch all users for a folder where the user was created after a certain date. the relationship between the user and the folder lives in a seperate table.

This is the query I came up with but it thorws the exception

No explicit selection and an implicit one cold not be determined

The code

@Override
public List<RetailPostUserTbl> getNewUsersForSiteSince( Date date, Integer siteId )
{
    List<RetailPostUserTbl> toReturn = new ArrayList<RetailPostUserTbl>();
    EntityManager em = getEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();

    Class<RpUserFolderMapTbl> userFolderPC = userFolderMapDAO.getPersistentClass();

    CriteriaQuery<RpUserFolderMapTbl> mapQuery = cb.createQuery( userFolderPC );
    Root<RpUserFolderMapTbl> root = mapQuery.from( userFolderPC );
    Path<Integer> folderIdPath = root.get( RpUserFolderMapTbl_.folder ).get( FolderTbl_.folderId );

    Predicate folderCondition = cb.equal( folderIdPath, siteId );

    Subquery<RetailPostUserTbl> rpSubQ = mapQuery.subquery( persistentClass );
    Root<RetailPostUserTbl> subQRoot = rpSubQ.from( persistentClass );
    Path<UserTbl> userPath = subQRoot.get( RetailPostUserTbl_.user );
    Path<Date> userCreatedPath = userPath.get( UserTbl_.userCreateDate );
    Predicate userCreateDateCondition = cb.greaterThanOrEqualTo( userCreatedPath, date );
    rpSubQ.where( userCreateDateCondition );

    mapQuery.where( cb.and( folderCondition, cb.exists( rpSubQ ) ) );

    TypedQuery<RpUserFolderMapTbl> query = em.createQuery( mapQuery );
    List<RpUserFolderMapTbl> results = query.getResultList();
    for ( RpUserFolderMapTbl result : results )
    {
        RetailPostUserTbl rpuser = result.getUser().getRetailPostUser();
        toReturn.add( rpuser );
    }
    return toReturn;
}

Anyone know why this is not working?

Answer

gokhansari picture gokhansari · Sep 16, 2014

You should set explicitly selection also for "subqueries".

rpSubQ.select(subQRoot);