The OPTIMIZE clause is not valid in JPQL/HQL. I believe you might want to rewrite your query using @NamedNativeQuery where you provide a native SQL query to utilize native SQL syntax with OPTIMIZE.