Title: Generated Columns
Many databases allow users to define special columns with values that are automatically created by the the database to be unique for each inserted row. Main use if this feature is to generate a primary key. Such columns are often called "autoincrement" or "identity" columns. JDBC3 specification provides Java API to read values of such columns right after an insert is performed. JDBC calls such columns "generated keys", and Cayenne follows JDBC in that. Namely DbAttribute has a "generated" boolean property to indicate that a given column value is provided by the database on insert.
Primary key generation is the only use of generated columns in Cayenne.
If a primary key column is marked as "generated" in CayenneModeler (see below), Cayenne will bypass its default PK generation mechanism and rely on the database to provide a key value. But only if the runtime DbAdapter is configured to allow that (see "Hints and Limitations" section for explanation). Currently only Derby, MySQL and SQLServer adapters allow generated columns by default. For the rest of the adapters, Cayenne will fall back to its default PK generation mechanism.
If you know that your driver supports generated keys API, but Cayenne assumes it does not, you can change this setting manually:
DataNode node = ...
JdbcAdapter adapter = (JdbcAdapter) node.getAdapter();
adapter.setSupportsGeneratedKeys(true);
Use of generated keys for PK is configured for each DbEntity individually in CayenneModeler:
MS SQLServer Note MS drivers prior to 2005 version do not support generated columns, while newer drivers and jTDS support it just fine. Cayenne can automatically detect the driver type and configure SQLServerAdapter, but only when AutoAdapter is used. To make sure auto-detection works, clear the "Custom Adapter" field for the corresponding DataNode in the Modeler. |