I have the following sql query which works as expected
Query query = entityManager.createNativeQuery("" + "SELECT d.id FROM document d \n" + "INNER JOIN users_documents ud \n" + "ON ud.document_id = d.id\n" + "WHERE ( d.document_state_id=2 OR d.document_state_id=3 OR d.document_state_id=1)\n" + "AND d.title LIKE \"%abc%\" AND ud.user_id=4 AND ud.sharing_type_id=1\n" + "OR (d.document_state_id=1 AND d.title LIKE \"%abc%\" AND ud.user_id=4 AND ud.sharing_type_id=2);"); And I have this JPQL which DOES not work, it returns nothing always.
TypedQuery<Long> q1 = entityManager.createQuery("Select d.id FROM Document d " + "INNER JOIN d.documentsForUsers ud" + " WHERE ((d.documentState.id=2 OR d.documentState.id=3 OR d.documentState.id=1)" + " AND (d.title LIKE :value2 AND ud.user.id=:id AND ud.sharingType.id=2))" + " OR (d.documentState.id=1 AND d.title LIKE :value3 AND ud.user.id=:id2 AND ud.sharingType.id=2)" , Long.class); q1.setParameter("value2", "%"+name+"%"); q1.setParameter("id", userId); q1.setParameter("value3", "%"+name+"%"); q1.setParameter("id2", userId); How can I rewrite that nativeSQL query to JPQL to make it work properly?
WHEREclause from the JPQL query, do you get any results back?