Example usage for org.hibernate.mapping PrimaryKey PrimaryKey

List of usage examples for org.hibernate.mapping PrimaryKey PrimaryKey

Introduction

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

Prototype

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);/*from   w ww  .j a v a 2 s .  c om*/
    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);//www.  j  a  va  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: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  w  ww  .  j a  va  2 s.co  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;
}

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

License:Apache License

public AlterTableBuilder addPrimaryKey(String... cols) {
    PrimaryKey pk = new PrimaryKey();
    for (String col : cols) {
        pk.addColumn(new Column(col));
    }//  w  w w .j  a  va2s .co  m
    alterFragments.add("add " + pk.sqlConstraintString(dialect));
    return this;
}

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

License:Apache License

public AlterTableBuilder replacePrimaryKey(String... cols) {
    PrimaryKey pk = new PrimaryKey();
    for (String col : cols) {
        pk.addColumn(new Column(col));
    }/*www. ja v  a2  s .  com*/
    if (dialectExtension.supportsPrimaryKeyReplace()) {
        alterFragments.add("drop primary key, add " + pk.sqlConstraintString(dialect));
    } else {
        dropPrimaryKey().addPrimaryKey(cols);
    }
    return this;
}

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

License:Apache License

public PrimaryKey getPrimaryKey() {
    PrimaryKey result = new PrimaryKey();
    for (String pk : primaryKeys)
        result.addColumn(new Column(pk));
    return result;
}

From source file:org.apereo.portal.tools.dbloader.TableXmlHandler.java

License:Apache License

@Override
public void endElement(String uri, String localName, String name) throws SAXException {
    if ("table".equals(name)) {
        for (final Column column : this.currentColumns.values()) {
            this.currentTable.addColumn(column);
        }/*from  w w  w .j a  va 2  s  .  c  o  m*/

        if (this.primaryKey != null) {
            this.currentTable.setPrimaryKey(this.primaryKey);
        }

        this.tables.put(this.currentTable.getName(), this.currentTable);
        this.tableColumnTypes.put(this.currentTable.getName(), this.currentColumnTypes);
        this.primaryKey = null;
        this.currentColumns = null;
        this.currentColumnTypes = null;
        this.currentTable = null;
    } else if ("column".equals(name)) {
        this.currentColumns.put(this.currentColumn.getName(), this.currentColumn);
        this.currentColumn = null;
    } else if ("name".equals(name)) {
        final String itemName = this.chars.toString().trim();

        if (this.currentIndex != null) {
            this.currentIndex.setName(itemName);
        } else if (this.currentUnique != null) {
            this.currentUnique.setName(itemName);
        } else if (this.currentTable == null) {
            this.currentTable = new Table(itemName);
        } else if (this.currentColumn == null) {
            this.currentColumn = new Column(itemName);
        }
    } else if ("type".equals(name)) {
        final String sqlTypeName = this.chars.toString().trim();

        final int sqlType = this.getSqlType(sqlTypeName);
        this.currentColumnTypes.put(this.currentColumn.getName(), sqlType);

        final String hibType = this.getHibernateType(sqlType);

        final SimpleValue value = new SimpleValue(this.mappings, this.currentTable);
        value.setTypeName(hibType);

        this.currentColumn.setValue(value);
    } else if ("param".equals(name)) {
        final String param = this.chars.toString().trim();

        final Integer length = Integer.valueOf(param);
        this.currentColumn.setLength(length);
    } else if ("primary-key".equals(name)) {
        final String columnName = this.chars.toString().trim();

        if (this.primaryKey == null) {
            this.primaryKey = new PrimaryKey();
        }

        final Column column = this.currentColumns.get(columnName);
        this.primaryKey.addColumn(column);
    } else if ("not-null".equals(name)) {
        final String columnName = this.chars.toString().trim();
        final Column column = this.currentColumns.get(columnName);
        column.setNullable(false);
    } else if ("column-ref".equals(name)) {
        final String columnName = this.chars.toString().trim();
        final Column column = this.currentColumns.get(columnName);

        if (this.currentIndex != null) {
            this.currentIndex.addColumn(column);
        } else if (this.currentUnique != null) {
            this.currentUnique.addColumn(column);
        }
    } else if ("index".equals(name)) {
        this.currentTable.addIndex(this.currentIndex);
        this.currentIndex = null;
    } else if ("unique".equals(name)) {
        this.currentTable.addUniqueKey(this.currentUnique);
        this.currentUnique = null;
    } else if ("key".equals(name)) {
        this.logger.warn("the 'key' element is ignored, use the table level 'primary-key' element instead");
    }

    this.chars = null;
}

From source file:org.processbase.engine.bam.db.ScriptGenerator.java

License:Open Source License

public ArrayList<String> getCreateTableScript(MetaKpi metaKpi, int type) {

    try {//from  ww w.  j av a 2s .c  o m
        Configuration config = new Configuration();

        Dialect d = (Dialect) Class.forName(BAMConstants.BAM_DB_DIALECT).newInstance();

        Mappings mappings = config.createMappings();
        table = mappings.addTable(null, null, metaKpi.getCode(), null, false);

        Column id = new Column("ID");
        SimpleValue idVal = new SimpleValue(table);
        idVal.setTypeName(TypeFactory.basic("long").getName());
        id.setValue(idVal);

        Column kpiTimeStamp = new Column("KPI_TIMESTAMP");
        kpiTimeStamp.setSqlType(TypeFactory.basic(java.util.Date.class.getName()).getName());

        Column kpiYear = new Column("KPI_YEAR");
        SimpleValue kpiYearVal = new SimpleValue(table);
        kpiYearVal.setTypeName(TypeFactory.basic("short").getName());
        kpiYear.setLength(4);
        kpiYear.setValue(kpiYearVal);
        addIndex(kpiYear);

        Column kpiQuater = new Column("KPI_QUATER");
        SimpleValue kpiQuaterVal = new SimpleValue(table);
        kpiQuaterVal.setTypeName(TypeFactory.basic("short").getName());
        kpiQuater.setLength(4);
        kpiQuater.setValue(kpiQuaterVal);
        addIndex(kpiQuater);

        Column kpiMonth = new Column("KPI_MONTH");
        SimpleValue kpiMonthVal = new SimpleValue(table);
        kpiMonthVal.setTypeName(TypeFactory.basic("short").getName());
        kpiMonth.setLength(4);
        kpiMonth.setValue(kpiMonthVal);
        addIndex(kpiMonth);

        Column kpiWeek = new Column("KPI_WEEK");
        SimpleValue kpiWeekVal = new SimpleValue(table);
        kpiWeekVal.setTypeName(TypeFactory.basic("short").getName());
        kpiWeek.setLength(4);
        kpiWeek.setValue(kpiWeekVal);
        addIndex(kpiWeek);

        Column kpiDay = new Column("KPI_DAY");
        SimpleValue kpiDayVal = new SimpleValue(table);
        kpiDayVal.setTypeName(TypeFactory.basic("short").getName());
        kpiDay.setLength(4);
        kpiDay.setValue(kpiDayVal);
        addIndex(kpiDay);

        Column kpiHour = new Column("KPI_HOUR");
        SimpleValue kpiHourVal = new SimpleValue(table);
        kpiHourVal.setTypeName(TypeFactory.basic("short").getName());
        kpiHour.setLength(4);
        kpiHour.setValue(kpiHourVal);
        addIndex(kpiHour);

        Column kpiMinute = new Column("KPI_MINUTE");
        SimpleValue kpiMinuteVal = new SimpleValue(table);
        kpiMinuteVal.setTypeName(TypeFactory.basic("short").getName());
        kpiMinute.setLength(4);
        kpiMinute.setValue(kpiMinuteVal);
        addIndex(kpiMinute);

        Column kpiDayOfWeek = new Column("KPI_DAY_OF_WEEK");
        SimpleValue kpiDayOfWeekVal = new SimpleValue(table);
        kpiDayOfWeekVal.setTypeName(TypeFactory.basic("short").getName());
        kpiDayOfWeek.setLength(4);
        kpiDayOfWeek.setValue(kpiDayOfWeekVal);
        addIndex(kpiDayOfWeek);

        Column serverId = new Column("SERVER_ID");
        SimpleValue serverIdVal = new SimpleValue(table);
        serverIdVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        serverId.setValue(serverIdVal);
        serverId.setLength(200);
        addIndex(serverId);

        Column eventId = new Column("EVENT_ID");
        SimpleValue eventIdVal = new SimpleValue(table);
        eventIdVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        eventId.setValue(eventIdVal);
        eventId.setLength(200);
        addIndex(eventId);

        Column eventName = new Column("EVENT_NAME");
        SimpleValue eventNameVal = new SimpleValue(table);
        eventNameVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        eventName.setValue(eventNameVal);
        eventName.setLength(200);
        addIndex(eventName);

        Column processDefinitionId = new Column("PROCESS_DEF_ID");
        SimpleValue processDefinitionIdVal = new SimpleValue(table);
        processDefinitionIdVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        processDefinitionId.setValue(processDefinitionIdVal);
        processDefinitionId.setLength(200);
        addIndex(processDefinitionId);

        Column processDefinitionName = new Column("PROCESS_DEF_NAME");
        SimpleValue processDefinitionNameVal = new SimpleValue(table);
        processDefinitionNameVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        processDefinitionName.setValue(processDefinitionNameVal);
        processDefinitionName.setLength(200);
        addIndex(processDefinitionName);

        Column processDefinitionVersion = new Column("PROCESS_DEF_VERSION");
        SimpleValue processDefinitionVersionVal = new SimpleValue(table);
        processDefinitionVersionVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        processDefinitionVersion.setValue(processDefinitionVersionVal);
        processDefinitionVersion.setLength(200);
        addIndex(processDefinitionVersion);

        Column processInstanceId = new Column("PROCESS_INST_ID");
        SimpleValue processInstanceIdVal = new SimpleValue(table);
        processInstanceIdVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        processInstanceId.setValue(processInstanceIdVal);
        processInstanceId.setLength(200);
        addIndex(processInstanceId);

        Column activityInstanceId = new Column("ACT_INST_ID");
        SimpleValue activityInstanceIdVal = new SimpleValue(table);
        activityInstanceIdVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        activityInstanceId.setValue(activityInstanceIdVal);
        activityInstanceId.setLength(200);
        addIndex(activityInstanceId);

        Column activityInstanceName = new Column("ACT_INST_NAME");
        SimpleValue activityInstanceNameVal = new SimpleValue(table);
        activityInstanceNameVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        activityInstanceName.setValue(activityInstanceNameVal);
        activityInstanceName.setLength(200);
        addIndex(activityInstanceName);

        Column activityInstanceIter = new Column("ACT_INST_ITER");
        SimpleValue activityInstanceIterVal = new SimpleValue(table);
        activityInstanceIterVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        activityInstanceIter.setValue(activityInstanceIterVal);
        activityInstanceIter.setLength(200);
        addIndex(activityInstanceIter);

        PrimaryKey key = new PrimaryKey();
        key.setName(metaKpi.getCode() + "_PK");
        key.addColumn(id);
        table.setPrimaryKey(key);

        table.addColumn(id);
        table.addColumn(kpiTimeStamp);
        table.addColumn(kpiYear);
        table.addColumn(kpiQuater);
        table.addColumn(kpiMonth);
        table.addColumn(kpiWeek);
        table.addColumn(kpiDay);
        table.addColumn(kpiDayOfWeek);
        table.addColumn(kpiHour);
        table.addColumn(kpiMinute);
        table.addColumn(serverId);
        table.addColumn(eventId);
        table.addColumn(eventName);
        table.addColumn(processDefinitionId);
        table.addColumn(processDefinitionVersion);
        table.addColumn(processDefinitionName);
        table.addColumn(processInstanceId);
        table.addColumn(activityInstanceId);
        table.addColumn(activityInstanceName);
        table.addColumn(activityInstanceIter);

        for (Iterator<MetaDim> i = metaKpi.getMetaDims().iterator(); i.hasNext();) {
            MetaDim metaDim = i.next();
            Column dim = new Column(metaDim.getCode());
            SimpleValue dimVal = new SimpleValue(table);
            dimVal.setTypeName(TypeFactory.basic(metaDim.getValueType()).getName());
            dim.setValue(dimVal);
            if (metaDim.getValueLength() != null) {
                dim.setLength(metaDim.getValueLength());
            }
            table.addColumn(dim);
            addIndex(dim);
        }

        for (Iterator<MetaFact> i = metaKpi.getMetaFacts().iterator(); i.hasNext();) {
            MetaFact metaFact = i.next();
            Column dim = new Column(metaFact.getCode());
            SimpleValue dimVal = new SimpleValue(table);
            dimVal.setTypeName(TypeFactory.basic("java.math.BigDecimal").getName());
            dim.setValue(dimVal);
            table.addColumn(dim);
        }

        if (type == CREATE_SCRIPT) {
            result.add(0, table.sqlCreateString(d, config.buildMapping(), null, null));
        } else if (type == DROP_SCRIPT) {
            result.clear();
            result.add(table.sqlDropString(d, null, null));
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return result;
}