0

I'm going to write this query, it works in Console.sql or in Workbench, but when I use it in @Query I get an error of syntax!

unexpected token: LIMIT


@Query("select w.name ,
                (select d.name 
                from document d 
                where w.id = d.workspace.id 
                order by (d.id) DESC 
                LIMIT 1 
                ) as nameLastDoc  
        from Workspace w  
            left join UserWorkspace u on u.workspace.id = w.id 
            left join Document d on w.id = d.workspace.id 
        group by (w.id)")

    List<?> findAllWorkspaces();

In my IDE the error is in the next select select w.name ,(select d.name ...

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
munir mounir
  • 249
  • 1
  • 3
  • 13

2 Answers2

1

As far as I worked with JPA Repository, it's quite limited (compared to a direct query to the database)

You can make a query directly in the Controller / where is your method :

// Autowiring the JdbcTemplate so we can send queries to the database
@Autowired
JdbcTemplate jdbcTemplate;

// Mock method that returns what it got from the database
public List<Map<String, Object> findAllWorkspaces() {
String query = "select w.name ,
            (select d.name 
            from document d 
            where w.id = d.workspace.id 
            order by (d.id) DESC 
            LIMIT 1 
            ) as nameLastDoc  
    from Workspace w  
        left join UserWorkspace u on u.workspace.id = w.id 
        left join Document d on w.id = d.workspace.id 
    group by (w.id)";

return jdbcTemplate.queryForList(query);
}

It returns a list of Map<String, Object>, where the string is the column's name, and the Object is the value from the database.

I used this in my application because I had to make custom queries and it worked. If this doesn't work either, your selection query is broken.

0

I dont know the exact error you are getting but I faced an error while using limit in JPA query. You can try removing it once to see if that is the issue. Also refer this thread.