java - Difference between RETURN_GENERATED_KEYS and specifying the generated column names -
what difference between preparestatement(string sql, int autogeneratedkeys)
, preparestatement(string sql, string[] columnnames
) methods of jdbc connection
class?
the javadoc both indicates returned preparedstatement
object capable of returning auto-generated keys if sql statement insert
statement. in case of first api, statement.return_generated_keys
needs passed autogeneratedkeys
parameter. in case of second api, names of generated columns passed string array.
what reasons using 1 on other?
i noticed spring's simplejdbcinsert
class prefers variant column names specified: abstractjdbcinsert.preparestatementforgeneratedkeys
why that?
the reasons convenience, flexibility, performance , compatibility. example, database cannot know columns auto-generated or not, default drivers return all columns when using statement.return_generated_keys
.
this can have impact on performance because:
- all values need transferred database client,
- on databases requires query on metadata know columns fetch.
for example, driver postgresql append returning *
(so has point 1 worry about), while firebird driver (which maintain) has query metadata.
some database drivers default return column not directly useful (eg oracle - used to? - return rowid
, means have query actual field yourself), , databases return primary key, while there might other generated fields, , believe database drivers return last generated key, if table doesn't use identity field(!).
the methods preparestatement(string sql, string[] columnnames)
, preparestatement(string sql, int[] columnindexes)
give more control (if supported), returned. if know fields need or want, can specify them , fields, without having worry differences in behavior return_generated_keys
.
depending on implementation, 1 taking string[] columnnames
efficient names can put in verbatim, while int[] columnindexes
might still require metadata query actual names.
Comments
Post a Comment