0

My problem seems to be very simple but I have hard time resolving it.
I want to alter an PostgreSQL sequence using Hibernate via native sql query (other solutions are also welcomed) using next code:

    Query query = getSession()
        .createSQLQuery("ALTER SEQUENCE users_id_seq RESTART WITH ?")
        .setInteger(0, 1);
    query.executeUpdate();

But I am getting this error:

Hibernate: 
    ALTER SEQUENCE users_id_seq RESTART WITH ?
hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 42601
hibernate.engine.jdbc.spi.SqlExceptionHelper - ERROR: syntax error at or near "$1"

1 Answer 1

1

Best I'm aware, you cannot prepare that statement.

If hibernate allows it, emulate the prepared statement, instead of sending it to the server. If not, sanitize the variable and issue the final statement directly.

Alternatively, wrap it in a function with dynamic SQL:

http://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

Sign up to request clarification or add additional context in comments.

3 Comments

I know is not a good practice, but probably I will go with the final statement directly, of course after sanitizing the input parameter.
Fwiw, altering a sequence is a bit weird too. :-)
Actually I perform some DBUnit testing, that's why I need it for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.