0

this is my repository:

@Repository
public interface EventRepository extends JpaRepository<Events, Long>, JpaSpecificationExecutor<Events>{

    @Query(value=" SELECT DISTINCT event.EVENT_ID "
                + "FROM EVENTS event "
                + "JOIN EVENTS event2 "
                + "ON event.EVENT_ID = event2.PARENT_ID "
                + "WHERE event.ENTITY_ID IN (?1)", nativeQuery = true)
    List<Long> getDescendantEventIdInEntityId(Set<Long> descendantEntities);
}

Why when this method is called, it's return: Internal Exception: java.sql.SQLException: Tipo di colonna non valido Error Code: 17004 Call: SELECT DISTINCT event.EVENT_ID FROM EVENTS event JOIN EVENTS event2 ON event.EVENT_ID = event2.PARENT_ID WHERE event.ENTITY_ID IN (?) bind => [[1]]

what is wrong with the query? If the query is ok, is that "bind => [[1]]" the true problem? If yes, why there's too many square brackets?

I've also tried this solution:

@Query(value=" SELECT DISTINCT event2.EVENT_ID "
                + "FROM EVENTS event "
                + "JOIN EVENTS event2 "
                + "ON event.EVENT_ID = event2.PARENT_ID "
                + "WHERE event.ENTITY_ID IN :entitiesId", nativeQuery = true)
    List<Long> getDescendantEventIdInEntityId(@Param("entitiesId") Set<Long> entitiesId);

but it return me another error: Missing IN or OUT parameter at index:: 1 Error Code: 17041 Call: SELECT DISTINCT event2.EVENT_ID FROM EVENTS event JOIN EVENTS event2 ON event.EVENT_ID = event2.PARENT_ID WHERE event.ENTITY_ID IN :entitiesId

SOLUTION: Solved creating a new nativeQuery from EntityManager:

em.createNativeQuery(" SELECT DISTINCT event2.EVENT_ID "
                                        + "FROM EVENTS event "
                                        + "JOIN EVENTS event2 "
                                        + "ON event.EVENT_ID = event2.PARENT_ID "
                                        + "WHERE event.ENTITY_ID IN ("+idList+") AND event.STATUS = 2")
                                          .getResultList();

Where idList is a String made by StringUtils.join(CollectionOfId, ",");

Innet
  • 459
  • 3
  • 5
  • 18

1 Answers1

2

If Hibernate was used as JPA provider, instead of positional parameter binding (?1) you could also use named parameter binding (:parameterName). This would be less error prone and you could omit the brackets and let Spring manage the syntax for your. You also would not need to parse anything on your own.

Use named parameter binding like:

@Query(value="SELECT DISTINCT event2.EVENT_ID "
        + "FROM EVENTS event "
        + "JOIN EVENTS event2 "
        + "ON event.EVENT_ID = event2.PARENT_ID "
        + "WHERE event.ENTITY_ID IN :entityIds", nativeQuery = true)
List<Long> getDescendantEventIdInEntityId(@Param("entityIds") Set<Long> ids);

Be aware of passing null or an empty set to the method, in both cases! That could cause unpredictable results.

Sadly, named parameters are not a JPA standard and maybe EclipseLink does not support it (in this way). Then you have to go with positional parameters.

Kevin Peters
  • 3,314
  • 1
  • 17
  • 38
  • i have already tried this solutions, but it return me another type of error. Check on my updated question – Innet Apr 10 '17 at 07:42
  • Can you tell us the full qualifier for your Param annotation? Is it org.springframework.data.repository.query.Param? It seems the passed value is missing in your case. – Kevin Peters Apr 10 '17 at 08:00
  • This is the import row for the param annotation: _import org.springframework.data.repository.query.Param;_ i'm pretty sure that the value is not null – Innet Apr 10 '17 at 09:27
  • 2
    Can you tell me which JPA provider is in use? Named parameters are not a standard in JPA within native queries, I only know that Hibernate does support it. – Kevin Peters Apr 10 '17 at 11:08
  • Just noticed that this is a eclipselink bug. Here there's a workaround for this case: https://bugs.eclipse.org/bugs/show_bug.cgi?id=328378 – Innet Apr 11 '17 at 08:23
  • That's sad. Maybe you'll find a workaround or there will be a fix. Or maybe an EclipseLink expert could help here. – Kevin Peters Apr 11 '17 at 09:35