Example usage for javax.persistence GenerationType IDENTITY

List of usage examples for javax.persistence GenerationType IDENTITY

Introduction

In this page you can find the example usage for javax.persistence GenerationType IDENTITY.

Prototype

GenerationType IDENTITY

To view the source code for javax.persistence GenerationType IDENTITY.

Click Source Link

Document

Indicates that the persistence provider must assign primary keys for the entity using a database identity column.

Usage

From source file:org.normandra.cassandra.CassandraDatabase.java

@Override
public void refresh() throws NormandraException {
    if (null == meta) {
        throw new NullArgumentException("database metadata");
    }/*from w  ww.j  a  va2  s  .c o  m*/

    if (DatabaseConstruction.NONE.equals(this.constructionMode)) {
        return;
    }

    // create all entity tables
    for (final String table : meta.getTables()) {
        try {
            this.refreshEntityTable(table, meta);
        } catch (final Exception e) {
            throw new NormandraException("Unable to refresh entity table [" + table + "].", e);
        }
    }

    // setup any table sequence/id generators
    for (final EntityMeta entity : meta) {
        for (final Class<?> entityType : entity.getTypes()) {
            final AnnotationParser parser = new AnnotationParser(new BasicColumnAccessorFactory(), entityType);
            for (final Map.Entry<Field, GeneratedValue> entry : parser.getGenerators(entityType).entrySet()) {
                final Field field = entry.getKey();
                final GeneratedValue generator = entry.getValue();
                final String type = generator.generator();
                String tableName = "id_generator";
                String keyColumn = "id";
                String keyValue = CaseUtils.camelToSnakeCase(entity.getTable());
                String valueColumn = "value";

                if (GenerationType.TABLE.equals(generator.strategy())) {
                    for (final TableGenerator table : parser.findAnnotations(entityType,
                            TableGenerator.class)) {
                        if (type.equalsIgnoreCase(table.name())) {
                            if (!table.table().isEmpty()) {
                                tableName = table.table();
                            }
                            if (!table.pkColumnName().isEmpty()) {
                                keyColumn = table.pkColumnName();
                            }
                            if (!table.pkColumnValue().isEmpty()) {
                                keyValue = table.pkColumnValue();
                            }
                            if (!table.valueColumnName().isEmpty()) {
                                valueColumn = table.valueColumnName();
                            }
                        }
                    }
                } else if (GenerationType.SEQUENCE.equals(generator.strategy())) {
                    for (final SequenceGenerator sequence : parser.findAnnotations(entityType,
                            SequenceGenerator.class)) {
                        if (type.equalsIgnoreCase(sequence.name())) {
                            if (!sequence.sequenceName().isEmpty()) {
                                keyValue = sequence.sequenceName();
                            }
                        }
                    }
                } else if (GenerationType.IDENTITY.equals(generator.strategy())) {
                    throw new NormandraException(
                            "Cassandra CQL3 does not support identity primary key generation.");
                }

                try {
                    this.refreshTableGenerator(tableName, keyColumn, valueColumn);
                } catch (final Exception e) {
                    throw new NormandraException(
                            "Unable to refresh id generator [" + generator.generator() + "].", e);
                }

                final String fieldName = field.getName();
                final ColumnMeta column = entity.findColumn(fieldName);
                if (column != null) {
                    final CassandraCounterIdGenerator counter = new CassandraCounterIdGenerator(tableName,
                            keyColumn, valueColumn, keyValue, this);
                    entity.setGenerator(column, counter);
                    logger.info("Set counter id generator for [" + column + "] on entity [" + entity + "].");
                    break;
                }
            }
        }
    }
}

From source file:org.normandra.orientdb.data.OrientDatabase.java

private void refreshGenerators(final EntityMeta entity, final ODatabaseDocument database) {
    // setup any table sequence/id generators
    for (final Class<?> entityType : entity.getTypes()) {
        final AnnotationParser parser = new AnnotationParser(new OrientAccessorFactory(), entityType);
        for (final Map.Entry<Field, GeneratedValue> entry : parser.getGenerators(entityType).entrySet()) {
            final Field field = entry.getKey();
            final GeneratedValue generator = entry.getValue();
            final String type = generator.generator();
            String tableName = "id_generator";
            String keyColumn = "id";
            String keyValue = CaseUtils.camelToSnakeCase(entity.getName());
            String valueColumn = "value";

            if (GenerationType.TABLE.equals(generator.strategy())) {
                for (final TableGenerator table : parser.findAnnotations(entityType, TableGenerator.class)) {
                    if (type.equalsIgnoreCase(table.name())) {
                        if (!table.table().isEmpty()) {
                            tableName = table.table();
                        }//from w  w w .  j a va  2 s  .com
                        if (!table.pkColumnName().isEmpty()) {
                            keyColumn = table.pkColumnName();
                        }
                        if (!table.pkColumnValue().isEmpty()) {
                            keyValue = table.pkColumnValue();
                        }
                        if (!table.valueColumnName().isEmpty()) {
                            valueColumn = table.valueColumnName();
                        }
                    }
                }
            } else if (GenerationType.SEQUENCE.equals(generator.strategy())) {
                for (final SequenceGenerator sequence : parser.findAnnotations(entityType,
                        SequenceGenerator.class)) {
                    if (type.equalsIgnoreCase(sequence.name())) {
                        if (!sequence.sequenceName().isEmpty()) {
                            keyValue = sequence.sequenceName();
                        }
                    }
                }
            } else if (GenerationType.IDENTITY.equals(generator.strategy())) {
                throw new IllegalStateException(
                        "No support available for orient-db identity primary key generation.");
            }

            // get the column type
            final String fieldName = field.getName();
            final String indexName = tableName + "." + keyColumn;
            final ColumnMeta column = entity.findColumn(fieldName);
            if (null == column) {
                throw new IllegalStateException(
                        "Unable to locate primary key [" + fieldName + "] for entity [" + entity + "].");
            }

            // drop table as required
            if (DatabaseConstruction.RECREATE.equals(this.constructionMode)) {
                if (hasCluster(database, tableName)) {
                    database.command(new OCommandSQL("DELETE FROM " + tableName)).execute();
                    database.getMetadata().getSchema().dropClass(tableName);
                }
                if (hasIndex(database, indexName)) {
                    database.command(new OCommandSQL("DROP INDEX " + indexName)).execute();
                    database.getMetadata().getIndexManager().dropIndex(indexName);
                }
            }

            // create sequence schema
            final OClass schemaClass = database.getMetadata().getSchema().getOrCreateClass(tableName);
            if (!schemaClass.existsProperty(keyColumn)) {
                schemaClass.createProperty(keyColumn, OType.STRING);
            }
            if (!schemaClass.existsProperty(valueColumn)) {
                schemaClass.createProperty(valueColumn, OrientUtils.columnType(column));
            }
            if (!schemaClass.areIndexed(keyColumn)) {
                schemaClass.createIndex(indexName, OClass.INDEX_TYPE.UNIQUE, keyColumn);
            }

            // assign generator
            final IdGenerator counter = new OrientIdGenerator(tableName, indexName, keyColumn, valueColumn,
                    keyValue, this);
            entity.setGenerator(column, counter);
            logger.info("Set counter id generator for [" + column + "] on entity [" + entity + "].");
        }
    }
}

From source file:org.opencustomer.framework.db.vo.BaseVO.java

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
public Integer getId() {
    return id;
}

From source file:pl.umk.mat.zawodyweb.database.pojo.Users.java

/**
 * @return id
 */
@Basic
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public Integer getId() {
    return id;
}