PostgreSQL next value of the sequences?

RETURNING That’s possible with a single round-trip to the database: INSERT INTO tbl(filename) VALUES (‘my_filename’) RETURNING tbl_id; tbl_id would typically be a serial or IDENTITY (Postgres 10 or later) column. More in the manual. Explicitly fetch value If filename needs to include tbl_id (redundantly), you can still use a single query. Use lastval() or the … Read more

get next sequence value from database using hibernate

You can use Hibernate Dialect API for Database independence as follow class SequenceValueGetter { private SessionFactory sessionFactory; // For Hibernate 3 public Long getId(final String sequenceName) { final List<Long> ids = new ArrayList<Long>(1); sessionFactory.getCurrentSession().doWork(new Work() { public void execute(Connection connection) throws SQLException { DialectResolver dialectResolver = new StandardDialectResolver(); Dialect dialect = dialectResolver.resolveDialect(connection.getMetaData()); PreparedStatement preparedStatement = … Read more