Package org.apache.openjpa.jdbc.schema

Examples of org.apache.openjpa.jdbc.schema.PrimaryKey


        Column c1 = table.addColumn("c1");
        Column c2 = table.addColumn("c2");
        Table table2 = _schema.addTable("table2");
        Column c3 = table2.addColumn("c3");
        Column c4 = table2.addColumn("c4");
        PrimaryKey pk = table2.addPrimaryKey("pk");
        pk.addColumn(c3);
        pk.addColumn(c4);
        ForeignKey fk = table.addForeignKey("fk");
        fk.join(c1, c3);
        fk.join(c2, c4);

        table2.removePrimaryKey();
        assertNull(pk.getTable());
        assertNull(table2.getPrimaryKey());
        assertEquals(0, table.getForeignKeys().length);
    }
View Full Code Here


     */
    public void testColumnRemoval() {
        Table table = _schema.addTable("table");
        Column c1 = table.addColumn("c1");
        Column c2 = table.addColumn("c2");
        PrimaryKey pk = table.addPrimaryKey("pk");
        pk.addColumn(c1);
        Index idx1 = table.addIndex("idx1");
        idx1.addColumn(c1);
        Index idx2 = table.addIndex("idx2");
        idx2.addColumn(c1);
        idx2.addColumn(c2);

        Table table2 = _schema.addTable("table2");
        Column c3 = table2.addColumn("c3");
        Column c4 = table2.addColumn("c4");
        pk = table2.addPrimaryKey("pk2");
        pk.addColumn(c3);
        ForeignKey fk = table.addForeignKey("fk");
        fk.join(c1, c3);

        table.removeColumn(c1);
        assertNull(table.getPrimaryKey());
View Full Code Here

    /**
     * Test that primary keys are resolved correctly.
     */
    public void testPrimaryKeyParsing() {
        Table table = _group.getSchema("SCHEMA1").getTable("TABLE1");
        PrimaryKey pk = table.getPrimaryKey();
        assertNotNull(pk);
        assertEquals("PK1", pk.getName());
        assertTrue(pk.isLogical());
        assertEquals(1, pk.getColumns().length);
        assertEquals(table.getColumn("COL1"), pk.getColumns()[0]);
       
        table = _group.getSchema("SCHEMA2").getTable("TABLE2");
        pk = table.getPrimaryKey();
        assertNotNull(pk);
        assertEquals("PK2", pk.getName());
        assertTrue(!pk.isLogical());
        assertEquals(2, pk.getColumns().length);
        assertEquals(table.getColumn("COL1"), pk.getColumns()[0]);
        assertEquals(table.getColumn("COL2"), pk.getColumns()[1]);
    }
View Full Code Here

            RelationStrategies.mapRelationToUnmappedPC(field, field.getName(),
                adapt);

        field.setUseClassCriteria(criteria);
        field.mapPrimaryKey(adapt);
        PrimaryKey pk = field.getTable().getPrimaryKey();
        if (field.isPrimaryKey()) {
            Column[] cols = field.getColumns();
            if (pk != null && (adapt || pk.isLogical()))
                for (int i = 0; i < cols.length; i++)
                    pk.addColumn(cols[i]);
            for (int i = 0; i < cols.length; i++)
                field.getDefiningMapping().setJoinable(cols[i], this);
        }

        // map constraints after pk so we don't re-index / re-unique pk col
View Full Code Here

    /**
     * Return whether the given foreign key is unique.
     */
    public boolean isUnique(ForeignKey fk) {
        PrimaryKey pk = fk.getTable().getPrimaryKey();
        if (pk != null && pk.columnsMatch(fk.getColumns()))
            return true;
        Index[] idx = fk.getTable().getIndexes();
        for (int i = 0; i < idx.length; i++)
            if (idx[i].isUnique() && idx[i].columnsMatch(fk.getColumns()))
                return true;
View Full Code Here

     * If the given table has a single unique foreign key or a foreign
     * key that matches the primary key, return it. Else return null.
     */
    public ForeignKey getUniqueForeignKey(Table table) {
        ForeignKey[] fks = table.getForeignKeys();
        PrimaryKey pk = table.getPrimaryKey();
        ForeignKey unq = null;
        int count = 0;
        for (int i = 0; i < fks.length; i++) {
            if (pk != null && pk.columnsMatch(fks[i].getColumns()))
                return fks[i];
            if (!isUnique(fks[i]))
                continue;

            count++;
View Full Code Here

                mapForeignKey(cls, fks[i], join, outer);

        // map any columns not controlled by foreign keys; also force primary
        // key cols to get mapped to simple fields even if the columns are
        // also foreign key columns
        PrimaryKey pk = (join != null) ? null : table.getPrimaryKey();
        boolean pkcol;
        Column[] cols = table.getColumns();
        for (int i = 0; i < cols.length; i++) {
            pkcol = pk != null && pk.containsColumn(cols[i]);
            if (pkcol && cls.getIdentityType() == ClassMapping.ID_DATASTORE)
                continue;
            if ((cls.getPCSuperclass() == null && pkcol)
                || !isForeignKeyColumn(cols[i]))
                mapColumn(cls, cols[i], join, outer);
View Full Code Here

            if (i > 0)
                buf.append(", ");
            buf.append(getDeclareColumnSQL(cols[i], false));
        }

        PrimaryKey pk = table.getPrimaryKey();
        String pkStr;
        if (pk != null) {
            pkStr = getPrimaryKeyConstraintSQL(pk);
            if (!StringUtils.isEmpty(pkStr))
                buf.append(", ").append(pkStr);
View Full Code Here

            if (i > 0)
                buf.append(", ");
            buf.append(getDeclareColumnSQL(cols[i], false));
        }

        PrimaryKey pk = table.getPrimaryKey();
        String pkStr;
        if (pk != null) {
            pkStr = getPrimaryKeyConstraintSQL(pk);
            if (!StringUtils.isEmpty(pkStr))
                buf.append(", ").append(pkStr);
View Full Code Here

   
    @Override
    public boolean needsToCreateIndex(Index idx, Table table, Unique[] uniques) {
       // SolidDB will automatically create a unique index for the
       // constraint, so don't create another index again
       PrimaryKey pk = table.getPrimaryKey();
       if (pk != null && idx.columnsMatch(pk.getColumns()))
           return false;
      
       // If table1 has constraints on column (a, b), an explicit index on (a)
       // will cause duplicate index error from SolidDB
       Column[] icols = idx.getColumns();
View Full Code Here

TOP

Related Classes of org.apache.openjpa.jdbc.schema.PrimaryKey

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.