Package org.jpox.store.rdbms.key

Examples of org.jpox.store.rdbms.key.CandidateKey


            {
                JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
                UniqueMetaData umd = embFieldMapping.getFieldMetaData().getUniqueMetaData();
                if (umd != null)
                {
                    CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, embFieldMapping);
                    if (ck != null)
                    {
                        candidateKeys.add(ck);
                    }
                }
View Full Code Here


                          continue;
                      }
   
                      String keyName=rsIndexes.getString(6);
                      DatastoreIdentifier idxName = idFactory.newIdentifier(IdentifierFactory.CANDIDATE_KEY, keyName);
                      CandidateKey can = (CandidateKey) candidateKeysByName.get(idxName);
                      if (can == null)
                      {
                          can = new CandidateKey(this);
                          can.setName(keyName);
     
                          candidateKeysByName.put(idxName, can);
                      }
     
                      // Set the column
                      int colSeq = rsIndexes.getShort(8) - 1;
                      DatastoreIdentifier colName = idFactory.newIdentifier(IdentifierFactory.COLUMN, rsIndexes.getString(9));
                      Column col = (Column) columnsByName.get(colName);
     
                      if (col != null)
                      {
                          can.setDatastoreField(colSeq, col);
                      }
                      }
                        hasNext = rsIndexes.next();
                }
                }
View Full Code Here

        Iterator i = expectedCandidateKeys.iterator();
        int n = 1;
        RDBMSIdentifierFactory idFactory = (RDBMSIdentifierFactory)storeMgr.getIdentifierFactory();
        while (i.hasNext())
        {
            CandidateKey ck = (CandidateKey) i.next();
            if (!actualCandidateKeysByName.containsValue(ck))
            {
                // If no name assigned, make one up
                if (ck.getName() == null)
                {
                    // Use the CandidateKeyIdentifier to generate the name
                    DatastoreIdentifier ckName;
                    do
                    {
                        ckName = idFactory.newCandidateKeyIdentifier(this, n++);
                    }
                    while (actualCandidateKeysByName.containsKey(ckName));
                    ck.setName(ckName.getIdentifier());
                }
                String stmtText = dba.getAddCandidateKeyStatement(ck, idFactory);
                stmtsByCKName.put(ck.getName(), stmtText);
            }
        }

        return stmtsByCKName;
    }
View Full Code Here

                {
                    JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
                    UniqueMetaData umd = embFieldMapping.getFieldMetaData().getUniqueMetaData();
                    if (umd != null)
                    {
                        CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, embFieldMapping);
                        if (ck != null)
                        {
                            candidateKeys.add(ck);
                        }
                    }
                }
            }
            else
            {
                // Add any required candidate key for this field
                UniqueMetaData umd = fmd.getUniqueMetaData();
                if (umd != null)
                {
                    CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, fieldMapping);
                    if (ck != null)
                    {
                        candidateKeys.add(ck);
                    }
                }
            }
        }

        // Add on any user-required candidate keys for the class(es) as a whole (composite keys)
        Iterator cmdIter = managedClassMetaData.iterator();
        while (cmdIter.hasNext())
        {
            ClassMetaData thisCmd = (ClassMetaData)cmdIter.next();
            UniqueMetaData[] classCKs = thisCmd.getUniqueMetaData();
            if (classCKs != null)
            {
                for (int i=0;i<classCKs.length;i++)
                {
                    CandidateKey ck = getCandidateKeyForUniqueMetaData(classCKs[i]);
                    if (ck != null)
                    {
                        candidateKeys.add(ck);
                    }
                }
View Full Code Here

     * @param umd The Unique MetaData
     * @return The Candidate Key
     */
    private CandidateKey getCandidateKeyForUniqueMetaData(UniqueMetaData umd)
    {
        CandidateKey ck = new CandidateKey(this);

        // Set the key name if required
        if (umd.getName() != null)
        {
            ck.setName(umd.getName());
        }

        // Class-level index so use its column definition
        ColumnMetaData[] colmds = umd.getColumnMetaData();
        AbstractMemberMetaData[] fmds = umd.getMemberMetaData();
        // a). Columns specified directly
        if (colmds != null && colmds.length > 0)
        {
            for (int i=0;i<colmds.length;i++)
            {
                DatastoreIdentifier colName = storeMgr.getIdentifierFactory().newDatastoreFieldIdentifier(colmds[i].getName());
                Column col = (Column)columnsByName.get(colName);
                if (col == null)
                {
                    JPOXLogger.DATASTORE.warn(LOCALISER.msg("058202",
                        toString(), ck.getName(), colmds[i].getName()));
                    break;
                }
                else
                {
                    ck.addDatastoreField(col);
                }
            }
        }
        // b). Columns specified using fields
        else if (fmds != null && fmds.length > 0)
        {
            for (int i=0;i<fmds.length;i++)
            {
                // Find the metadata for the actual field with the same name as this "unique" field
                AbstractMemberMetaData realFmd = getFieldMetaData(fmds[i].getName());
                JavaTypeMapping fieldMapping = (JavaTypeMapping)fieldMappingsMap.get(realFmd);
                int countFields = fieldMapping.getNumberOfDatastoreFields();
                for (int j=0; j<countFields; j++)
                {
                    ck.addDatastoreField(fieldMapping.getDataStoreMapping(j).getDatastoreField());
                }
            }
        }
        else
        {
            // We can't have an index of no columns
            JPOXLogger.DATASTORE.warn(LOCALISER.msg("058203",
                toString(), ck.getName()));
            return null;
        }

        return ck;
    }
View Full Code Here

        RDBMSIdentifierFactory idFactory = (RDBMSIdentifierFactory)storeMgr.getIdentifierFactory();
        while (cks.hasNext())
        {
            DatastoreIdentifier ckName = idFactory.newCandidateKeyIdentifier(this, ++ckNum);
            CandidateKey ck = (CandidateKey)cks.next();
            ck.setName(ckName.getIdentifier());

            stmts.add(dba.getAddCandidateKeyStatement(ck, idFactory));
        }

        return stmts;
View Full Code Here

                            // a unique constraint on them. If the key field is in a superclass then we
                            // cannot do this so just omit it.
                            if (keyMapping.getDatastoreContainer() == this &&
                                ownerMapping.getDatastoreContainer() == this)
                            {
                                CandidateKey ck = new CandidateKey(this);

                                // This HashSet is to avoid duplicate adding of columns.
                                HashSet addedColumns = new HashSet();

                                // Add columns for the owner field
                                int countOwnerFields = ownerMapping.getNumberOfDatastoreFields();
                                for (int i = 0; i < countOwnerFields; i++)
                                {
                                    Column col = (Column) ownerMapping.getDataStoreMapping(i).getDatastoreField();
                                    addedColumns.add(col);
                                    ck.addDatastoreField(col);
                                }

                                // Add columns for the key field
                                int countKeyFields = keyMapping.getNumberOfDatastoreFields();
                                for (int i = 0; i < countKeyFields; i++)
                                {
                                    Column col = (Column) keyMapping.getDataStoreMapping(i).getDatastoreField();
                                    if (!addedColumns.contains(col))
                                    {
                                        addedColumns.add(col);
                                        ck.addDatastoreField(col);
                                    }
                                    else
                                    {
                                        JPOXLogger.DATASTORE.warn(LOCALISER.msg("057041",
                                            ownerfmd.getName()));
                                    }
                                }

                                if (candidateKeysByMapField.put(mfmd, ck) != null)
                                {
                                    // We have multiple "mapped-by" coming to this field so give a warning that this may potentially
                                    // cause problems. For example if they have the key field defined here for 2 different relations
                                    // so you may get keys/values appearing in the other relation that shouldn't be.
                                    // Logged as a WARNING for now.
                                    // If there is a situation where this should throw an exception, please update this AND COMMENT WHY.
                                    JPOXLogger.DATASTORE.warn(LOCALISER.msg("057012",
                                        mfmd.getFullFieldName(), ownerfmd.getFullFieldName()));
                                }
                            }
                        }
                    }
                    else if (ownerfmd.getValueMetaData() != null && ownerfmd.getValueMetaData().getMappedBy() != null)
                    {
                        // Value field is stored in the key table
                        AbstractMemberMetaData vmd = null;
                        String value_field_name = ownerfmd.getValueMetaData().getMappedBy();
                        if (value_field_name != null)
                        {
                            vmd = cmd.getMetaDataForMember(value_field_name);
                        }
                        if (vmd == null)
                        {
                            throw new ClassDefinitionException(LOCALISER.msg("057008", mfmd));
                        }

                        JavaTypeMapping ownerMapping = getFieldMapping(map_field_name);
                        JavaTypeMapping valueMapping = getFieldMapping(vmd.getName());

                        if (dba.supportsNullsInCandidateKeys() || (!ownerMapping.isNullable() && !valueMapping.isNullable()))
                        {
                            // If the owner and value fields are represented in this table then we can impose
                            // a unique constraint on them. If the value field is in a superclass then we
                            // cannot do this so just omit it.
                            if (valueMapping.getDatastoreContainer() == this &&
                                ownerMapping.getDatastoreContainer() == this)
                            {
                                CandidateKey ck = new CandidateKey(this);

                                // This HashSet is to avoid duplicate adding of columns.
                                HashSet addedColumns = new HashSet();

                                // Add columns for the owner field
                                int countOwnerFields = ownerMapping.getNumberOfDatastoreFields();
                                for (int i = 0; i < countOwnerFields; i++)
                                {
                                    Column col = (Column) ownerMapping.getDataStoreMapping(i).getDatastoreField();
                                    addedColumns.add(col);
                                    ck.addDatastoreField(col);
                                }

                                // Add columns for the value field
                                int countValueFields = valueMapping.getNumberOfDatastoreFields();
                                for (int i = 0; i < countValueFields; i++)
                                {
                                    Column col = (Column) valueMapping.getDataStoreMapping(i).getDatastoreField();
                                    if (!addedColumns.contains(col))
                                    {
                                        addedColumns.add(col);
                                        ck.addDatastoreField(col);
                                    }
                                    else
                                    {
                                        JPOXLogger.DATASTORE.warn(LOCALISER.msg("057042",
                                            ownerfmd.getName()));
View Full Code Here

     * @param fieldMapping Mapping for the field
     * @return The Candidate Key
     */
    public static CandidateKey getCandidateKeyForField(DatastoreContainerObject table, UniqueMetaData umd, JavaTypeMapping fieldMapping)
    {
        CandidateKey ck = new CandidateKey(table);

        // Set the key name if required
        if (umd.getName() != null)
        {
            IdentifierFactory idFactory = table.getStoreManager().getIdentifierFactory();
            DatastoreIdentifier ckId = idFactory.newIdentifier(IdentifierFactory.CANDIDATE_KEY, umd.getName());
            ck.setName(ckId.toString());
        }

        // Field-level index so use all columns for the field
        int countFields = fieldMapping.getNumberOfDatastoreFields();
        for (int j=0; j<countFields; j++)
        {
            ck.addDatastoreField(fieldMapping.getDataStoreMapping(j).getDatastoreField());
        }

        return ck;
    }
View Full Code Here

            {
                JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
                UniqueMetaData umd = embFieldMapping.getFieldMetaData().getUniqueMetaData();
                if (umd != null)
                {
                    CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, embFieldMapping);
                    if (ck != null)
                    {
                        candidateKeys.add(ck);
                    }
                }
            }
        }

        if (valueMapping instanceof EmbeddedValuePCMapping)
        {
            // Add all candidate keys required by fields of the embedded value
            EmbeddedValuePCMapping embMapping = (EmbeddedValuePCMapping)valueMapping;
            for (int i=0;i<embMapping.getNumberOfJavaTypeMappings();i++)
            {
                JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
                UniqueMetaData umd = embFieldMapping.getFieldMetaData().getUniqueMetaData();
                if (umd != null)
                {
                    CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, embFieldMapping);
                    if (ck != null)
                    {
                        candidateKeys.add(ck);
                    }
                }
View Full Code Here

TOP

Related Classes of org.jpox.store.rdbms.key.CandidateKey

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.