Search in sources :

Example 1 with AttributePath

use of org.hibernate.boot.model.source.spi.AttributePath in project hibernate-orm by hibernate.

the class AttributesHelper method processPropertiesGroup.

private static void processPropertiesGroup(final MappingDocument mappingDocument, final Callback callback, final JaxbHbmPropertiesType propertiesGroupJaxbMapping, String logicalTableName, NaturalIdMutability naturalIdMutability) {
    final String name = propertiesGroupJaxbMapping.getName();
    final AttributeRole attributeRole = callback.getAttributeSourceContainer().getAttributeRoleBase().append(name);
    final AttributePath attributePath = callback.getAttributeSourceContainer().getAttributePathBase().append(name);
    final EmbeddableSourceVirtualImpl embeddable = new EmbeddableSourceVirtualImpl(mappingDocument, callback, new EmbeddableSourceContainer() {

        @Override
        public AttributeRole getAttributeRoleBase() {
            return attributeRole;
        }

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

        @Override
        public ToolingHintContext getToolingHintContextBaselineForEmbeddable() {
            return callback.getAttributeSourceContainer().getToolingHintContext();
        }
    }, propertiesGroupJaxbMapping.getAttributes(), logicalTableName, naturalIdMutability, propertiesGroupJaxbMapping);
    // fake the JAXB mapping...
    final EmbeddableMapping embeddableMapping = new EmbeddableMapping() {

        @Override
        public String getClazz() {
            return null;
        }

        @Override
        public List<JaxbHbmTuplizerType> getTuplizer() {
            return Collections.emptyList();
        }

        @Override
        public String getParent() {
            return null;
        }
    };
    final EmbeddedAttributeMapping attributeMapping = new EmbeddedAttributeMapping() {

        @Override
        public boolean isUnique() {
            return propertiesGroupJaxbMapping.isUnique();
        }

        @Override
        public EmbeddableMapping getEmbeddableMapping() {
            return embeddableMapping;
        }

        @Override
        public String getName() {
            return propertiesGroupJaxbMapping.getName();
        }

        @Override
        public String getAccess() {
            return null;
        }

        @Override
        public List<JaxbHbmToolingHintType> getToolingHints() {
            return Collections.emptyList();
        }
    };
    // todo : make the virtual embedded attribute
    final SingularAttributeSourceEmbedded virtualAttribute = new AbstractSingularAttributeSourceEmbeddedImpl(mappingDocument, attributeMapping, embeddable, naturalIdMutability) {

        @Override
        public boolean isVirtualAttribute() {
            return true;
        }

        @Override
        public Boolean isInsertable() {
            return propertiesGroupJaxbMapping.isInsert();
        }

        @Override
        public Boolean isUpdatable() {
            return propertiesGroupJaxbMapping.isUpdate();
        }

        @Override
        public boolean isBytecodeLazy() {
            return false;
        }

        @Override
        public XmlElementMetadata getSourceType() {
            return XmlElementMetadata.PROPERTIES;
        }

        @Override
        public String getXmlNodeName() {
            return null;
        }

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

        @Override
        public AttributeRole getAttributeRole() {
            return attributeRole;
        }

        @Override
        public boolean isIncludedInOptimisticLocking() {
            return false;
        }

        @Override
        public ToolingHintContext getToolingHintContext() {
            return mappingDocument.getToolingHintContext();
        }
    };
    callback.addAttributeSource(virtualAttribute);
}
Also used : AttributeRole(org.hibernate.boot.model.source.spi.AttributeRole) SingularAttributeSourceEmbedded(org.hibernate.boot.model.source.spi.SingularAttributeSourceEmbedded) EmbeddedAttributeMapping(org.hibernate.boot.model.source.spi.EmbeddedAttributeMapping) JaxbHbmToolingHintType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmToolingHintType) JaxbHbmTuplizerType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmTuplizerType) ToolingHintContext(org.hibernate.boot.model.source.spi.ToolingHintContext) EmbeddableMapping(org.hibernate.boot.model.source.spi.EmbeddableMapping) AttributePath(org.hibernate.boot.model.source.spi.AttributePath)

Example 2 with AttributePath

use of org.hibernate.boot.model.source.spi.AttributePath 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 3 with AttributePath

use of org.hibernate.boot.model.source.spi.AttributePath 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();
            }
        });
        // HHH-11826 magic. See Ejb3Column and the HHH-6005 comments
        if (columnIdentifier.getText().contains("_collection&&element_")) {
            columnIdentifier = Identifier.toIdentifier(columnIdentifier.getText().replace("_collection&&element_", "_"), columnIdentifier.isQuoted());
        }
        // 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 4 with AttributePath

use of org.hibernate.boot.model.source.spi.AttributePath in project midpoint by Evolveum.

the class MidPointImplicitNamingStrategy method determineJoinColumnName.

@Override
public Identifier determineJoinColumnName(ImplicitJoinColumnNameSource source) {
    Identifier i = super.determineJoinColumnName(source);
    // RObject, creatorRef.target, oid -> m_object.creatorRef_targetOid
    // RObjectReference, owner, oid -> m_object_reference.owner_oid
    // RObjectReference, target, oid -> m_object_reference.targetOid
    AttributePath path = source.getAttributePath();
    String property = path.getProperty();
    String columnName = source.getReferencedColumnName().getText();
    Identifier real;
    if (path.getDepth() == 1) {
        String name;
        if (property.endsWith("target") && "oid".equals(columnName)) {
            name = property + "Oid";
        } else {
            name = StringUtils.join(Arrays.asList(property, columnName), "_");
        }
        real = toIdentifier(name, source.getBuildingContext());
    } else {
        // TODO fixme BRUTAL HACK -- we are not able to eliminate columns like 'ownerRefCampaign_targetOid' from the schema (even with @AttributeOverride/@AssociationOverride)
        if ("ownerRefCampaign.target".equals(path.getFullPath()) || "ownerRefDefinition.target".equals(path.getFullPath()) || "ownerRefTask.target".equals(path.getFullPath())) {
            path = AttributePath.parse("ownerRef.target");
        }
        AttributePath parent = path.getParent();
        String translatedParent = transformAttributePath(parent);
        columnName = property + StringUtils.capitalize(columnName);
        real = toIdentifier(StringUtils.join(Arrays.asList(translatedParent, columnName), "_"), source.getBuildingContext());
    }
    LOGGER.trace("determineJoinColumnName {} {} -> {}, {}", source.getReferencedTableName(), source.getReferencedColumnName(), i, real);
    return real;
}
Also used : AttributePath(org.hibernate.boot.model.source.spi.AttributePath)

Example 5 with AttributePath

use of org.hibernate.boot.model.source.spi.AttributePath in project hibernate-orm by hibernate.

the class AttributePathTest method testCollectionElement.

@Test
@TestForIssue(jiraKey = "HHH-10863")
public void testCollectionElement() {
    AttributePath attributePath = AttributePath.parse("items.collection&&element.name");
    assertFalse(attributePath.isCollectionElement());
    assertTrue(attributePath.getParent().isCollectionElement());
    assertFalse(attributePath.getParent().getParent().isCollectionElement());
}
Also used : AttributePath(org.hibernate.boot.model.source.spi.AttributePath) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

AttributePath (org.hibernate.boot.model.source.spi.AttributePath)5 Identifier (org.hibernate.boot.model.naming.Identifier)2 ImplicitNamingStrategy (org.hibernate.boot.model.naming.ImplicitNamingStrategy)2 PhysicalNamingStrategy (org.hibernate.boot.model.naming.PhysicalNamingStrategy)2 Database (org.hibernate.boot.model.relational.Database)2 JoinColumn (javax.persistence.JoinColumn)1 PrimaryKeyJoinColumn (javax.persistence.PrimaryKeyJoinColumn)1 AnnotationException (org.hibernate.AnnotationException)1 JaxbHbmToolingHintType (org.hibernate.boot.jaxb.hbm.spi.JaxbHbmToolingHintType)1 JaxbHbmTuplizerType (org.hibernate.boot.jaxb.hbm.spi.JaxbHbmTuplizerType)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 ObjectNameNormalizer (org.hibernate.boot.model.naming.ObjectNameNormalizer)1 AttributeRole (org.hibernate.boot.model.source.spi.AttributeRole)1 EmbeddableMapping (org.hibernate.boot.model.source.spi.EmbeddableMapping)1 EmbeddedAttributeMapping (org.hibernate.boot.model.source.spi.EmbeddedAttributeMapping)1 SingularAttributeSourceEmbedded (org.hibernate.boot.model.source.spi.SingularAttributeSourceEmbedded)1 ToolingHintContext (org.hibernate.boot.model.source.spi.ToolingHintContext)1