Search in sources :

Example 41 with SimpleValue

use of org.hibernate.mapping.SimpleValue in project hibernate-orm by hibernate.

the class AnnotationBinder method bindDiscriminatorColumnToRootPersistentClass.

private static void bindDiscriminatorColumnToRootPersistentClass(RootClass rootClass, Ejb3DiscriminatorColumn discriminatorColumn, Map<String, Join> secondaryTables, PropertyHolder propertyHolder, MetadataBuildingContext context) {
    if (rootClass.getDiscriminator() == null) {
        if (discriminatorColumn == null) {
            throw new AssertionFailure("discriminator column should have been built");
        }
        discriminatorColumn.setJoins(secondaryTables);
        discriminatorColumn.setPropertyHolder(propertyHolder);
        SimpleValue discriminatorColumnBinding = new SimpleValue(context.getMetadataCollector(), rootClass.getTable());
        rootClass.setDiscriminator(discriminatorColumnBinding);
        discriminatorColumn.linkWithValue(discriminatorColumnBinding);
        discriminatorColumnBinding.setTypeName(discriminatorColumn.getDiscriminatorTypeName());
        rootClass.setPolymorphic(true);
        if (LOG.isTraceEnabled()) {
            LOG.tracev("Setting discriminator for entity {0}", rootClass.getEntityName());
        }
    }
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) SimpleValue(org.hibernate.mapping.SimpleValue)

Example 42 with SimpleValue

use of org.hibernate.mapping.SimpleValue in project hibernate-orm by hibernate.

the class CopyIdentifierComponentSecondPass method createSimpleProperty.

private Property createSimpleProperty(PersistentClass referencedPersistentClass, boolean isExplicitReference, Map<String, Ejb3JoinColumn> columnByReferencedName, AtomicInteger index, Property referencedProperty) {
    Property property = new Property();
    property.setName(referencedProperty.getName());
    //FIXME set optional?
    //property.setOptional( property.isOptional() );
    property.setPersistentClass(component.getOwner());
    property.setPropertyAccessorName(referencedProperty.getPropertyAccessorName());
    SimpleValue value = new SimpleValue(buildingContext.getMetadataCollector(), component.getTable());
    property.setValue(value);
    final SimpleValue referencedValue = (SimpleValue) referencedProperty.getValue();
    value.setTypeName(referencedValue.getTypeName());
    value.setTypeParameters(referencedValue.getTypeParameters());
    final Iterator<Selectable> columns = referencedValue.getColumnIterator();
    if (joinColumns[0].isNameDeferred()) {
        joinColumns[0].copyReferencedStructureAndCreateDefaultJoinColumns(referencedPersistentClass, columns, value);
    } else {
        //FIXME take care of Formula
        while (columns.hasNext()) {
            final Selectable selectable = columns.next();
            if (!Column.class.isInstance(selectable)) {
                log.debug("Encountered formula definition; skipping");
                continue;
            }
            final Column column = (Column) selectable;
            final Ejb3JoinColumn joinColumn;
            String logicalColumnName = null;
            if (isExplicitReference) {
                final String columnName = column.getName();
                logicalColumnName = buildingContext.getMetadataCollector().getLogicalColumnName(referencedPersistentClass.getTable(), columnName);
                //JPA 2 requires referencedColumnNames to be case insensitive
                joinColumn = columnByReferencedName.get(logicalColumnName.toLowerCase(Locale.ROOT));
            } else {
                joinColumn = columnByReferencedName.get("" + index.get());
                index.getAndIncrement();
            }
            if (joinColumn == null && !joinColumns[0].isNameDeferred()) {
                throw new AnnotationException(isExplicitReference ? "Unable to find column reference in the @MapsId mapping: " + logicalColumnName : "Implicit column reference in the @MapsId mapping fails, try to use explicit referenceColumnNames: " + referencedEntityName);
            }
            final String columnName = joinColumn == null || joinColumn.isNameDeferred() ? "tata_" + column.getName() : joinColumn.getName();
            value.addColumn(new Column(columnName));
            if (joinColumn != null) {
                joinColumn.linkWithValue(value);
            }
            column.setValue(value);
        }
    }
    return property;
}
Also used : Selectable(org.hibernate.mapping.Selectable) Column(org.hibernate.mapping.Column) AnnotationException(org.hibernate.AnnotationException) Property(org.hibernate.mapping.Property) SimpleValue(org.hibernate.mapping.SimpleValue)

Aggregations

SimpleValue (org.hibernate.mapping.SimpleValue)42 Property (org.hibernate.mapping.Property)23 PersistentClass (org.hibernate.mapping.PersistentClass)18 Column (org.hibernate.mapping.Column)13 Test (org.junit.Test)12 AnnotationException (org.hibernate.AnnotationException)11 MetadataSources (org.hibernate.boot.MetadataSources)11 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)10 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)10 Identifier (org.hibernate.boot.model.naming.Identifier)9 Component (org.hibernate.mapping.Component)8 DependantValue (org.hibernate.mapping.DependantValue)8 BasicType (org.hibernate.type.BasicType)8 LocalMetadataBuildingContext (org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext)7 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)7 Table (org.hibernate.mapping.Table)7 RootClass (org.hibernate.mapping.RootClass)6 AssertionFailure (org.hibernate.AssertionFailure)5 Collection (org.hibernate.mapping.Collection)5 ManyToOne (org.hibernate.mapping.ManyToOne)5