how to use pg_column_size in hibernate?

Viewed 110

I am trying to get the size of a row in a postgresql table, I found that pg_column_size would do the trick, but its not working with hibernate :

@Query("SELECT pg_column_size(t.*) as filesize FROM TABLE as t where name=:name")
int getSize(@Param("name") String name);

intellij is giving this error :

< operator > or AS expected, got '('

I guess the problem is that hibernate doesnt support specific postgresql queries, it only supports the basic sql queries.

so is there a way around this ? if not is there a way to get/estimate the size of a postgresql row in java ?

2 Answers

In order to use a built-in postgres function , you have to declare you JPA query as nativeQuery

you should first change query to native (hibernate will directly execute the query instead of jpa -> sql generation )

@Query(value="SELECT pg_column_size(t.*) as filesize FROM TABLE as t where name=:name",nativeQuery=true)
int getSize(@Param("name") String name);

Also be sur of the TABLE name to be correct .

Add nativeQuery = true after the native query.

@Query("SELECT pg_column_size(t.*) as filesize FROM users as t where t.name=:name",nativeQuery = true)

use above Query, Hope This will work.

Related