0

I try to use a native query in my Dao (extended from JpaRepository), but i get this exception :

org.springframework.orm.jpa.JpaSystemException: Could not extract result set metadata; nested exception is org.hibernate.HibernateException: Could not extract result set metadata

Here is my native query:

@Repository
public interface BenchDao extends JpaRepository<Bench, Long> {


@Query(nativeQuery=true,value="UPDATE TF_BENCH SET ISDELETED = 1, SEQ = (select CASE WHEN (min(BENCH.SEQ) < 1) THEN (min(BENCH.SEQ)-1) ELSE -1 END from TF_BENCH BENCH WHERE BENCH.STAGE = (select BENCH.STAGE from TF_BENCH BENCH WHERE id =29302)) WHERE id =29302")
void deleteBench();

When i try this request on Oracle SQL developper it works, but through spring and hibernate it doesn't.

In the stack trace exeception I have also this message :

Caused by: java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL statement

I've tried to google exception messages, but I found nothing...

stephane06
  • 261
  • 1
  • 4
  • 12
  • Can you post the code about how you are invoking `deleteBench();` – Vinayak Pingale May 27 '14 at 12:35
  • Seems to be a problem with your Query, Have you tried to execute the query directly on database? – Jens May 27 '14 at 12:36
  • yes i tried, as i said : "When i try this request on Oracle SQL developper it works" – stephane06 May 27 '14 at 12:43
  • I'm invoking like this way : benchDao.deleteBench(); – stephane06 May 27 '14 at 12:43
  • Write a simple jdbc program and run this SQL on the database and see the results. If it does'nt work, start debugging from there. The actual error is thrown by Oracle database which is propagated to hibernate via jdbc driver so that would help you to diagnose. – Shailendra May 27 '14 at 12:45
  • About the way I invoking deleteBench() : In a spring Service class, with the Transaction annotation over the wrapping method – stephane06 May 27 '14 at 12:55
  • I have found a solution. (based on EntityManager.executeUpdate in a custom Dao implementation). I'll post it when I'll be allowded – stephane06 May 27 '14 at 13:17

2 Answers2

5

I think that a better solution was to add the @Modifying annotation to your method.

See more on the following Answer : Spring Data JPA delete native query throwing exception

Community
  • 1
  • 1
Lionel Père
  • 526
  • 2
  • 5
  • 17
2

I've found a solution : if someone need :

@Repository
public class BenchDaoCustomImpl implements BenchDaoCustom {

    @PersistenceContext
    private EntityManager em;

    public void deleteBench(Long benchId) {
        this.em.createNativeQuery("UPDATE TF_BENCH SET ISDELETED = 1, SEQ = (select CASE WHEN (min(BENCH.SEQ) < 1) THEN (min(BENCH.SEQ)-1) ELSE -1 END from TF_BENCH BENCH WHERE BENCH.STAGE = (select BENCH.STAGE from TF_BENCH BENCH WHERE id =?0)) WHERE id =?0")
            .setParameter(0, benchId)
            .executeUpdate();
    }
}   
stephane06
  • 261
  • 1
  • 4
  • 12