Example usage for org.hibernate.mapping Table setPrimaryKey

List of usage examples for org.hibernate.mapping Table setPrimaryKey

Introduction

In this page you can find the example usage for org.hibernate.mapping Table setPrimaryKey.

Prototype

public void setPrimaryKey(PrimaryKey primaryKey) 

Source Link

Usage

From source file:com.manydesigns.portofino.persistence.hibernate.HibernateConfig.java

License:Open Source License

protected void createPKComposite(Mappings mappings, com.manydesigns.portofino.model.database.Table mdTable,
        String pkName, RootClass clazz, Table tab,
        List<com.manydesigns.portofino.model.database.Column> columnPKList) {

    PrimaryKey primaryKey = new PrimaryKey();
    primaryKey.setName(pkName);/* w ww. jav  a 2  s. c o  m*/
    primaryKey.setTable(tab);

    clazz.setEmbeddedIdentifier(true);
    Component component = new Component(mappings, clazz);
    component.setDynamic(mdTable.getActualJavaClass() == null);
    String name;
    name = mdTable.getQualifiedName();

    component.setRoleName(name + ".id");
    component.setEmbedded(true);
    //component.setNodeName("id");
    component.setKey(true);
    component.setNullValue("undefined");

    if (!component.isDynamic()) {
        component.setComponentClassName(mdTable.getJavaClass()); //TODO verificare se non si intende actualJavaClass
    }

    boolean hasErrors = false;
    for (com.manydesigns.portofino.model.database.Column column : columnPKList) {
        if (column == null) {
            throw new InternalError("Null column");
        }

        Column col = createColumn(mappings, tab, column);

        hasErrors = col == null || hasErrors;

        if (col != null) {
            primaryKey.addColumn(col);
            Property prop = createProperty(column, col.getValue());
            prop.setCascade("none");
            //prop.setPropertyAccessorName("property"); interferisce con il generator pi sotto
            prop.setPersistentClass(clazz);
            component.addProperty(prop);

            //Generator not supported for embedded map identifier
            //See https://forum.hibernate.org/viewtopic.php?t=945273
            //See Component.buildIdentifierGenerator()
            /*String columnName = column.getColumnName();
            PrimaryKeyColumn pkCol = mdTable.getPrimaryKey().findPrimaryKeyColumnByName(columnName);
            if(pkCol == null) {
            logger.error("Column without corresponding PrimaryKeyColumn: {}", columnName);
            hasErrors = true;
            continue;
            }
            Generator generator = pkCol.getGenerator();
            setPKColumnGenerator(mappings, clazz, tab, column, value, generator);*/
        }
    }
    if (hasErrors) {
        // TODO PAOLO: se la PK non e' buona, tutta la tabella dovrebbe saltare
        logger.error("Skipping primary key");
        return;
    }

    tab.setIdentifierValue(component);
    clazz.setIdentifier(component);
    clazz.setDiscriminatorValue(name);

    tab.setPrimaryKey(primaryKey);
}

From source file:com.manydesigns.portofino.persistence.hibernate.HibernateConfig.java

License:Open Source License

protected void createPKSingle(Mappings mappings, com.manydesigns.portofino.model.database.Table mdTable,
        String pkName, RootClass clazz, Table tab,
        List<com.manydesigns.portofino.model.database.Column> columnPKList) {
    PrimaryKeyColumn pkcol = mdTable.getPrimaryKey().getPrimaryKeyColumns().get(0);
    com.manydesigns.portofino.model.database.Column column = columnPKList.get(0);
    final PrimaryKey primaryKey = new PrimaryKey();
    primaryKey.setName(pkName);/*from   w w w .j  ava  2  s . c  o  m*/
    primaryKey.setTable(tab);
    tab.setPrimaryKey(primaryKey);

    Column col = createColumn(mappings, tab, column);

    if (col == null) {
        // TODO PAOLO: se la PK non e' buona, tutta la tabella dovrebbe saltare
        logger.error("Skipping primary key");
        return;
    }

    SimpleValue id = (SimpleValue) col.getValue();
    //Make the defaults explicit. See section 5.1.4.5. Assigned identifiers in the Hibernate reference
    //(http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html)
    id.setIdentifierGeneratorStrategy("assigned");
    id.setNullValue("undefined");

    tab.getPrimaryKey().addColumn(col);

    Property prop = createProperty(column, id);
    clazz.addProperty(prop);
    prop.setPropertyAccessorName(mappings.getDefaultAccess());
    //PropertyGeneration generation = PropertyGeneration.parse(null);
    //prop.setGeneration(generation);

    prop.setInsertable(false);
    prop.setUpdateable(false);

    Generator generator = pkcol.getGenerator();

    setPKColumnGenerator(mappings, clazz, tab, column, id, generator);

    tab.setIdentifierValue(id);
    clazz.setIdentifier(id);
    clazz.setIdentifierProperty(prop);
    clazz.setDiscriminatorValue(mdTable.getQualifiedName());

}

From source file:com.zutubi.pulse.master.hibernate.SchemaRefactor.java

License:Apache License

protected Table clone(Table table) {
    Table clone = new Table(table.getName());
    clone.setAbstract(table.isAbstract());
    clone.setCatalog(table.getCatalog());
    clone.setComment(table.getComment());
    clone.setName(table.getName());//from  w  w w.ja  v  a 2s  .  c o  m
    clone.setPrimaryKey(table.getPrimaryKey());
    clone.setQuoted(table.isQuoted());
    clone.setRowId(table.getRowId());
    clone.setSchema(table.getSchema());
    clone.setSubselect(table.getSubselect());

    Iterator columns = table.getColumnIterator();
    while (columns.hasNext()) {
        Column column = (Column) columns.next();
        clone.addColumn(column);
    }

    Iterator foreignKeys = table.getForeignKeyIterator();
    while (foreignKeys.hasNext()) {
        ForeignKey key = (ForeignKey) foreignKeys.next();
        clone.createForeignKey(key.getName(), key.getColumns(), key.getReferencedEntityName(),
                key.getReferencedColumns());
    }

    return clone;
}

From source file:net.e6tech.elements.persist.hibernate.ModifiedTableGenerator.java

License:Apache License

@Override
public void registerExportables(Database database) {
    final Dialect dialect = database.getJdbcEnvironment().getDialect();

    final Namespace namespace = database.locateNamespace(qualifiedTableName.getCatalogName(),
            qualifiedTableName.getSchemaName());

    Table table = namespace.locateTable(qualifiedTableName.getObjectName());
    if (table == null) {
        table = namespace.createTable(qualifiedTableName.getObjectName(), false);

        // todo : note sure the best solution here.  do we add the columns if missing?  other?
        final Column segmentColumn = new ExportableColumn(database, table, segmentColumnName,
                StringType.INSTANCE, dialect.getTypeName(Types.VARCHAR, segmentValueLength, 0, 0));
        segmentColumn.setNullable(false);
        table.addColumn(segmentColumn);//from www . j a v a2  s.c o m

        // lol
        table.setPrimaryKey(new PrimaryKey(table));
        table.getPrimaryKey().addColumn(segmentColumn);

        final Column valueColumn = new ExportableColumn(database, table, valueColumnName, LongType.INSTANCE);
        table.addColumn(valueColumn);
    }

    // allow physical naming strategies a chance to kick in
    this.renderedTableName = database.getJdbcEnvironment().getQualifiedObjectNameFormatter()
            .format(table.getQualifiedTableName(), dialect);

    this.selectQuery = buildSelectQuery(dialect);
    this.updateQuery = buildUpdateQuery();
    this.insertQuery = buildInsertQuery();

}

From source file:net.lshift.hibernate.migrations.AlterTableBuilder.java

License:Apache License

public AlterTableBuilder addForeignKey(String name, String[] columnNames, String referencedTable,
        String[] referencedColumns) {
    ForeignKey fk = new ForeignKey();
    fk.setName(name);/*from   ww  w . java 2  s .c o m*/
    for (String col : columnNames)
        fk.addColumn(new Column(col));
    fk.setTable(new Table(table));

    PrimaryKey refPrimaryKey = new PrimaryKey();
    for (String col : referencedColumns)
        refPrimaryKey.addColumn(new Column(col));
    Table refTable = new Table(referencedTable);
    refTable.setPrimaryKey(refPrimaryKey);

    fk.setReferencedTable(refTable);

    String defaultCatalog = config.getProperties().getProperty(Environment.DEFAULT_CATALOG);
    String defaultSchema = config.getProperties().getProperty(Environment.DEFAULT_SCHEMA);

    // fk.sqlConstraintString appears to generate incorrect SQL against MySQL in some instances.
    // The referenced columns are not always correctly listed.
    //    alterFragments.add(" add index " + fk.getName() + " (" + StringHelper.join(", ", columnNames) +
    //        "), add constraint " + fk.getName() + " foreign key (" + StringHelper.join(", ", columnNames) +
    //        " references " + referencedTable + " (" + StringHelper.join(", ", referencedColumns) + ")");
    alterFragments.add(fk.sqlConstraintString(dialect, fk.getName(), defaultCatalog, defaultSchema));
    return this;
}