Stack Exchange
Pass sequence name as parameter in @Query JPA Oracle
I am trying to retrieve data from Oracle Sequence using JPA as below:
@Repository
public interface SequenceRepository extends JpaRepository<MyEntity, Long> {
    
    @Query(value = "SELECT %s.NEXTVAL FROM dual", nativeQuery = true)
    Long getSequenceValue(String sequenceName);
}

Calling above method from Service class as:
Long nextVal = sequenceRepository.getSequenceValue("MASTER.TestSequence");

While doing so, I am getting error as:

could not extract Resultset; nested exception is oracle.hibernate.exception.SQLGrammarException
oracle.jdbc.OracleDatabaseException: ORA-00911: invalid character

How to pass sequence name as argument to @Query & would it cause SQL injection?

In general, SQL treats query parameters as values, not identifiers. So you can't use a parameter for a sequence name, a table name, a column name, etc. You can only use a parameter in place of a single numeric value or string value.
