Search in sources :

Example 1 with PhysicalNamingStrategy

use of org.hibernate.boot.model.naming.PhysicalNamingStrategy in project hibernate-orm by hibernate.

the class Ejb3Column method redefineColumnName.

public void redefineColumnName(String columnName, String propertyName, boolean applyNamingStrategy) {
    final ObjectNameNormalizer normalizer = context.getObjectNameNormalizer();
    final Database database = context.getMetadataCollector().getDatabase();
    final ImplicitNamingStrategy implicitNamingStrategy = context.getBuildingOptions().getImplicitNamingStrategy();
    final PhysicalNamingStrategy physicalNamingStrategy = context.getBuildingOptions().getPhysicalNamingStrategy();
    if (applyNamingStrategy) {
        if (StringHelper.isEmpty(columnName)) {
            if (propertyName != null) {
                final AttributePath attributePath = AttributePath.parse(propertyName);
                Identifier implicitName = normalizer.normalizeIdentifierQuoting(implicitNamingStrategy.determineBasicColumnName(new ImplicitBasicColumnNameSource() {

                    @Override
                    public AttributePath getAttributePath() {
                        return attributePath;
                    }

                    @Override
                    public boolean isCollectionElement() {
                        // @Column refers to the element column
                        return !propertyHolder.isComponent() && !propertyHolder.isEntity();
                    }

                    @Override
                    public MetadataBuildingContext getBuildingContext() {
                        return context;
                    }
                }));
                // HHH-6005 magic
                if (implicitName.getText().contains("_collection&&element_")) {
                    implicitName = Identifier.toIdentifier(implicitName.getText().replace("_collection&&element_", "_"), implicitName.isQuoted());
                }
                final Identifier physicalName = physicalNamingStrategy.toPhysicalColumnName(implicitName, database.getJdbcEnvironment());
                mappingColumn.setName(physicalName.render(database.getDialect()));
            }
        //Do nothing otherwise
        } else {
            final Identifier explicitName = database.toIdentifier(columnName);
            final Identifier physicalName = physicalNamingStrategy.toPhysicalColumnName(explicitName, database.getJdbcEnvironment());
            mappingColumn.setName(physicalName.render(database.getDialect()));
        }
    } else {
        if (StringHelper.isNotEmpty(columnName)) {
            mappingColumn.setName(normalizer.toDatabaseIdentifierText(columnName));
        }
    }
}
Also used : ImplicitNamingStrategy(org.hibernate.boot.model.naming.ImplicitNamingStrategy) Identifier(org.hibernate.boot.model.naming.Identifier) Database(org.hibernate.boot.model.relational.Database) ObjectNameNormalizer(org.hibernate.boot.model.naming.ObjectNameNormalizer) ImplicitBasicColumnNameSource(org.hibernate.boot.model.naming.ImplicitBasicColumnNameSource) PhysicalNamingStrategy(org.hibernate.boot.model.naming.PhysicalNamingStrategy) AttributePath(org.hibernate.boot.model.source.spi.AttributePath)

Example 2 with PhysicalNamingStrategy

use of org.hibernate.boot.model.naming.PhysicalNamingStrategy in project hibernate-orm by hibernate.

the class Ejb3JoinColumn method buildDefaultColumnName.

private String buildDefaultColumnName(final PersistentClass referencedEntity, final String logicalReferencedColumn) {
    final Database database = getBuildingContext().getMetadataCollector().getDatabase();
    final ImplicitNamingStrategy implicitNamingStrategy = getBuildingContext().getBuildingOptions().getImplicitNamingStrategy();
    final PhysicalNamingStrategy physicalNamingStrategy = getBuildingContext().getBuildingOptions().getPhysicalNamingStrategy();
    Identifier columnIdentifier;
    boolean mappedBySide = mappedByTableName != null || mappedByPropertyName != null;
    boolean ownerSide = getPropertyName() != null;
    Boolean isRefColumnQuoted = StringHelper.isQuoted(logicalReferencedColumn);
    final String unquotedLogicalReferenceColumn = isRefColumnQuoted ? StringHelper.unquote(logicalReferencedColumn) : logicalReferencedColumn;
    if (mappedBySide) {
        // NOTE : While it is completely misleading here to allow for the combination
        //		of a "JPA ElementCollection" to be mappedBy, the code that uses this
        // 		class relies on this behavior for handling the inverse side of
        // 		many-to-many mappings
        final AttributePath attributePath = AttributePath.parse(mappedByPropertyName);
        final ImplicitJoinColumnNameSource.Nature implicitNamingNature;
        if (getPropertyHolder().isEntity()) {
            implicitNamingNature = ImplicitJoinColumnNameSource.Nature.ENTITY;
        } else if (JPA2ElementCollection) {
            implicitNamingNature = ImplicitJoinColumnNameSource.Nature.ELEMENT_COLLECTION;
        } else {
            implicitNamingNature = ImplicitJoinColumnNameSource.Nature.ENTITY_COLLECTION;
        }
        columnIdentifier = implicitNamingStrategy.determineJoinColumnName(new ImplicitJoinColumnNameSource() {

            private final EntityNaming entityNaming = new EntityNaming() {

                @Override
                public String getClassName() {
                    return referencedEntity.getClassName();
                }

                @Override
                public String getEntityName() {
                    return referencedEntity.getEntityName();
                }

                @Override
                public String getJpaEntityName() {
                    return referencedEntity.getJpaEntityName();
                }
            };

            private final Identifier referencedTableName = getBuildingContext().getMetadataCollector().getDatabase().toIdentifier(mappedByTableName);

            @Override
            public Nature getNature() {
                return implicitNamingNature;
            }

            @Override
            public EntityNaming getEntityNaming() {
                return entityNaming;
            }

            @Override
            public AttributePath getAttributePath() {
                return attributePath;
            }

            @Override
            public Identifier getReferencedTableName() {
                return referencedTableName;
            }

            @Override
            public Identifier getReferencedColumnName() {
                if (logicalReferencedColumn != null) {
                    return getBuildingContext().getMetadataCollector().getDatabase().toIdentifier(logicalReferencedColumn);
                }
                if (mappedByEntityName == null || mappedByPropertyName == null) {
                    return null;
                }
                final PersistentClass mappedByEntityBinding = getBuildingContext().getMetadataCollector().getEntityBinding(mappedByEntityName);
                final Property mappedByProperty = mappedByEntityBinding.getProperty(mappedByPropertyName);
                final SimpleValue value = (SimpleValue) mappedByProperty.getValue();
                final Iterator<Selectable> selectableValues = value.getColumnIterator();
                if (!selectableValues.hasNext()) {
                    throw new AnnotationException(String.format(Locale.ENGLISH, "mapped-by [%s] defined for attribute [%s] referenced an invalid property (no columns)", mappedByPropertyName, propertyHolder.getPath()));
                }
                final Selectable selectable = selectableValues.next();
                if (!Column.class.isInstance(selectable)) {
                    throw new AnnotationException(String.format(Locale.ENGLISH, "mapped-by [%s] defined for attribute [%s] referenced an invalid property (formula)", mappedByPropertyName, propertyHolder.getPath()));
                }
                if (selectableValues.hasNext()) {
                    throw new AnnotationException(String.format(Locale.ENGLISH, "mapped-by [%s] defined for attribute [%s] referenced an invalid property (multiple columns)", mappedByPropertyName, propertyHolder.getPath()));
                }
                return getBuildingContext().getMetadataCollector().getDatabase().toIdentifier(((Column) selectable).getQuotedName());
            }

            @Override
            public MetadataBuildingContext getBuildingContext() {
                return Ejb3JoinColumn.this.getBuildingContext();
            }
        });
        //one element was quoted so we quote
        if (isRefColumnQuoted || StringHelper.isQuoted(mappedByTableName)) {
            columnIdentifier = Identifier.quote(columnIdentifier);
        }
    } else if (ownerSide) {
        final String logicalTableName = getBuildingContext().getMetadataCollector().getLogicalTableName(referencedEntity.getTable());
        final ImplicitJoinColumnNameSource.Nature implicitNamingNature;
        if (JPA2ElementCollection) {
            implicitNamingNature = ImplicitJoinColumnNameSource.Nature.ELEMENT_COLLECTION;
        } else if (getPropertyHolder().isEntity()) {
            implicitNamingNature = ImplicitJoinColumnNameSource.Nature.ENTITY;
        } else {
            implicitNamingNature = ImplicitJoinColumnNameSource.Nature.ENTITY_COLLECTION;
        }
        columnIdentifier = getBuildingContext().getBuildingOptions().getImplicitNamingStrategy().determineJoinColumnName(new ImplicitJoinColumnNameSource() {

            private final EntityNaming entityNaming = new EntityNaming() {

                @Override
                public String getClassName() {
                    return referencedEntity.getClassName();
                }

                @Override
                public String getEntityName() {
                    return referencedEntity.getEntityName();
                }

                @Override
                public String getJpaEntityName() {
                    return referencedEntity.getJpaEntityName();
                }
            };

            private final AttributePath attributePath = AttributePath.parse(getPropertyName());

            private final Identifier referencedTableName = getBuildingContext().getMetadataCollector().getDatabase().toIdentifier(logicalTableName);

            private final Identifier referencedColumnName = getBuildingContext().getMetadataCollector().getDatabase().toIdentifier(logicalReferencedColumn);

            @Override
            public Nature getNature() {
                return implicitNamingNature;
            }

            @Override
            public EntityNaming getEntityNaming() {
                return entityNaming;
            }

            @Override
            public AttributePath getAttributePath() {
                return attributePath;
            }

            @Override
            public Identifier getReferencedTableName() {
                return referencedTableName;
            }

            @Override
            public Identifier getReferencedColumnName() {
                return referencedColumnName;
            }

            @Override
            public MetadataBuildingContext getBuildingContext() {
                return Ejb3JoinColumn.this.getBuildingContext();
            }
        });
        //one element was quoted so we quote
        if (isRefColumnQuoted || StringHelper.isQuoted(logicalTableName)) {
            columnIdentifier = Identifier.quote(columnIdentifier);
        }
    } else {
        final Identifier logicalTableName = database.toIdentifier(getBuildingContext().getMetadataCollector().getLogicalTableName(referencedEntity.getTable()));
        // is an intra-entity hierarchy table join so copy the name by default
        columnIdentifier = implicitNamingStrategy.determinePrimaryKeyJoinColumnName(new ImplicitPrimaryKeyJoinColumnNameSource() {

            @Override
            public MetadataBuildingContext getBuildingContext() {
                return Ejb3JoinColumn.this.getBuildingContext();
            }

            @Override
            public Identifier getReferencedTableName() {
                return logicalTableName;
            }

            @Override
            public Identifier getReferencedPrimaryKeyColumnName() {
                return database.toIdentifier(logicalReferencedColumn);
            }
        });
        if (!columnIdentifier.isQuoted() && (isRefColumnQuoted || logicalTableName.isQuoted())) {
            columnIdentifier = Identifier.quote(columnIdentifier);
        }
    }
    return physicalNamingStrategy.toPhysicalColumnName(columnIdentifier, database.getJdbcEnvironment()).render(database.getJdbcEnvironment().getDialect());
}
Also used : ImplicitNamingStrategy(org.hibernate.boot.model.naming.ImplicitNamingStrategy) ImplicitJoinColumnNameSource(org.hibernate.boot.model.naming.ImplicitJoinColumnNameSource) ImplicitPrimaryKeyJoinColumnNameSource(org.hibernate.boot.model.naming.ImplicitPrimaryKeyJoinColumnNameSource) MetadataBuildingContext(org.hibernate.boot.spi.MetadataBuildingContext) PhysicalNamingStrategy(org.hibernate.boot.model.naming.PhysicalNamingStrategy) SimpleValue(org.hibernate.mapping.SimpleValue) Identifier(org.hibernate.boot.model.naming.Identifier) Selectable(org.hibernate.mapping.Selectable) JoinColumn(javax.persistence.JoinColumn) Column(org.hibernate.mapping.Column) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) EntityNaming(org.hibernate.boot.model.naming.EntityNaming) Database(org.hibernate.boot.model.relational.Database) AnnotationException(org.hibernate.AnnotationException) Property(org.hibernate.mapping.Property) AttributePath(org.hibernate.boot.model.source.spi.AttributePath) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 3 with PhysicalNamingStrategy

use of org.hibernate.boot.model.naming.PhysicalNamingStrategy in project hibernate-orm by hibernate.

the class Ejb3Column method buildColumnFromAnnotation.

public static Ejb3Column[] buildColumnFromAnnotation(javax.persistence.Column[] anns, org.hibernate.annotations.Formula formulaAnn, Nullability nullability, PropertyHolder propertyHolder, PropertyData inferredData, String suffixForDefaultColumnName, Map<String, Join> secondaryTables, MetadataBuildingContext context) {
    Ejb3Column[] columns;
    if (formulaAnn != null) {
        Ejb3Column formulaColumn = new Ejb3Column();
        formulaColumn.setFormula(formulaAnn.value());
        formulaColumn.setImplicit(false);
        formulaColumn.setBuildingContext(context);
        formulaColumn.setPropertyHolder(propertyHolder);
        formulaColumn.bind();
        columns = new Ejb3Column[] { formulaColumn };
    } else {
        javax.persistence.Column[] actualCols = anns;
        javax.persistence.Column[] overriddenCols = propertyHolder.getOverriddenColumn(StringHelper.qualify(propertyHolder.getPath(), inferredData.getPropertyName()));
        if (overriddenCols != null) {
            //check for overridden first
            if (anns != null && overriddenCols.length != anns.length) {
                throw new AnnotationException("AttributeOverride.column() should override all columns for now");
            }
            actualCols = overriddenCols.length == 0 ? null : overriddenCols;
            LOG.debugf("Column(s) overridden for property %s", inferredData.getPropertyName());
        }
        if (actualCols == null) {
            columns = buildImplicitColumn(inferredData, suffixForDefaultColumnName, secondaryTables, propertyHolder, nullability, context);
        } else {
            final int length = actualCols.length;
            columns = new Ejb3Column[length];
            for (int index = 0; index < length; index++) {
                final ObjectNameNormalizer normalizer = context.getObjectNameNormalizer();
                final Database database = context.getMetadataCollector().getDatabase();
                final ImplicitNamingStrategy implicitNamingStrategy = context.getBuildingOptions().getImplicitNamingStrategy();
                final PhysicalNamingStrategy physicalNamingStrategy = context.getBuildingOptions().getPhysicalNamingStrategy();
                javax.persistence.Column col = actualCols[index];
                final String sqlType;
                if (col.columnDefinition().equals("")) {
                    sqlType = null;
                } else {
                    sqlType = normalizer.applyGlobalQuoting(col.columnDefinition());
                }
                final String tableName;
                if (StringHelper.isEmpty(col.table())) {
                    tableName = "";
                } else {
                    tableName = database.getJdbcEnvironment().getIdentifierHelper().toIdentifier(col.table()).render();
                //						final Identifier logicalName = database.getJdbcEnvironment()
                //								.getIdentifierHelper()
                //								.toIdentifier( col.table() );
                //						final Identifier physicalName = physicalNamingStrategy.toPhysicalTableName( logicalName );
                //						tableName = physicalName.render( database.getDialect() );
                }
                final String columnName;
                if ("".equals(col.name())) {
                    columnName = null;
                } else {
                    // NOTE : this is the logical column name, not the physical!
                    columnName = database.getJdbcEnvironment().getIdentifierHelper().toIdentifier(col.name()).render();
                }
                Ejb3Column column = new Ejb3Column();
                if (length == 1) {
                    applyColumnDefault(column, inferredData);
                }
                column.setImplicit(false);
                column.setSqlType(sqlType);
                column.setLength(col.length());
                column.setPrecision(col.precision());
                column.setScale(col.scale());
                if (StringHelper.isEmpty(columnName) && !StringHelper.isEmpty(suffixForDefaultColumnName)) {
                    column.setLogicalColumnName(inferredData.getPropertyName() + suffixForDefaultColumnName);
                } else {
                    column.setLogicalColumnName(columnName);
                }
                column.setPropertyName(BinderHelper.getRelativePath(propertyHolder, inferredData.getPropertyName()));
                column.setNullable(col.nullable());
                //TODO force to not null if available? This is a (bad) user choice.
                column.setUnique(col.unique());
                column.setInsertable(col.insertable());
                column.setUpdatable(col.updatable());
                column.setExplicitTableName(tableName);
                column.setPropertyHolder(propertyHolder);
                column.setJoins(secondaryTables);
                column.setBuildingContext(context);
                column.extractDataFromPropertyData(inferredData);
                column.bind();
                columns[index] = column;
            }
        }
    }
    return columns;
}
Also used : ImplicitNamingStrategy(org.hibernate.boot.model.naming.ImplicitNamingStrategy) ObjectNameNormalizer(org.hibernate.boot.model.naming.ObjectNameNormalizer) PhysicalNamingStrategy(org.hibernate.boot.model.naming.PhysicalNamingStrategy) Column(org.hibernate.mapping.Column) Database(org.hibernate.boot.model.relational.Database) AnnotationException(org.hibernate.AnnotationException)

Aggregations

ImplicitNamingStrategy (org.hibernate.boot.model.naming.ImplicitNamingStrategy)3 PhysicalNamingStrategy (org.hibernate.boot.model.naming.PhysicalNamingStrategy)3 Database (org.hibernate.boot.model.relational.Database)3 AnnotationException (org.hibernate.AnnotationException)2 Identifier (org.hibernate.boot.model.naming.Identifier)2 ObjectNameNormalizer (org.hibernate.boot.model.naming.ObjectNameNormalizer)2 AttributePath (org.hibernate.boot.model.source.spi.AttributePath)2 Column (org.hibernate.mapping.Column)2 JoinColumn (javax.persistence.JoinColumn)1 PrimaryKeyJoinColumn (javax.persistence.PrimaryKeyJoinColumn)1 EntityNaming (org.hibernate.boot.model.naming.EntityNaming)1 ImplicitBasicColumnNameSource (org.hibernate.boot.model.naming.ImplicitBasicColumnNameSource)1 ImplicitJoinColumnNameSource (org.hibernate.boot.model.naming.ImplicitJoinColumnNameSource)1 ImplicitPrimaryKeyJoinColumnNameSource (org.hibernate.boot.model.naming.ImplicitPrimaryKeyJoinColumnNameSource)1 MetadataBuildingContext (org.hibernate.boot.spi.MetadataBuildingContext)1 PersistentClass (org.hibernate.mapping.PersistentClass)1 Property (org.hibernate.mapping.Property)1 Selectable (org.hibernate.mapping.Selectable)1 SimpleValue (org.hibernate.mapping.SimpleValue)1