Home > Enterprise >  Query from pgAdmin doesn't work in JPARepository
Query from pgAdmin doesn't work in JPARepository

Time:06-01

I'm trying to implement simple Join query where a take id's from the main Entity and names from other entities using id's from first Entity. Everything works fine in the pgAdmin's query tool, but JPA repository doesn't allow to execute this query. Any ideas?

@Transactional
    @Query(nativeQuery = true, value = "select battery.id,battery.uuid,battery.name,battery.price, battery.second_price,\n"  
            "capacity.name as capacity_name, monoblock.name as monoblock_name,\n"  
            "manufacturer_country.name as manufacturer_country_name,\n"  
            "battery_level_name.name as battery_level_name,\n"  
            "manufacturer.name as manufacturer_name,\n"  
            "battery.inrush_current,battery.size, battery.image_url, polarity.name as polarity_name\n"  
            "from battery\n"  
            "JOIN capacity ON battery.capacity_id = Capacity.id\n"  
            "JOIN monoblock ON battery.monoblock_id = Monoblock.id \n"  
            "JOIN manufacturer_country ON battery.manufacturer_country_id = manufacturer_country.id\n"  
            "JOIN manufacturer ON battery.manufacturer_id = manufacturer.id\n"  
            "JOIN battery_level_name ON battery.battery_level_name_id = battery_level_name.id\n"  
            "JOIN polarity ON monoblock.polarity_id = polarity.id")
    Page<Battery> getPaginatingBatteries(Pageable pageable);

Error:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause

org.postgresql.util.PSQLException: ERROR: syntax error (example position: ".")

Sometime query gives this error:

org.postgresql.util.PSQLException: Column battery_level_name_id not found in this ResultSet.

CodePudding user response:

Remove all

\n

by space, you will have a better result.

CodePudding user response:

return Page<Object[]> instead of Page<Battery>. Replace \n with a space at the end.

  • Related