Search in sources :

Example 16 with Selectable

use of org.hibernate.mapping.Selectable 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, 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) {
                applyComponentColumnSizeValueToJoinColumn(column, joinColumn);
                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)

Example 17 with Selectable

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

the class MapBinder method makeOneToManyMapKeyColumnNullableIfNotInProperty.

private void makeOneToManyMapKeyColumnNullableIfNotInProperty(final XProperty property) {
    final org.hibernate.mapping.Map map = (org.hibernate.mapping.Map) this.collection;
    if (map.isOneToMany() && property.isAnnotationPresent(MapKeyColumn.class)) {
        final Value indexValue = map.getIndex();
        if (indexValue.getColumnSpan() != 1) {
            throw new AssertionFailure("Map key mapped by @MapKeyColumn does not have 1 column");
        }
        final Selectable selectable = indexValue.getColumnIterator().next();
        if (selectable.isFormula()) {
            throw new AssertionFailure("Map key mapped by @MapKeyColumn is a Formula");
        }
        Column column = (Column) map.getIndex().getColumnIterator().next();
        if (!column.isNullable()) {
            final PersistentClass persistentClass = ((OneToMany) map.getElement()).getAssociatedClass();
            // need to check "un-joined" properties.
            if (!propertyIteratorContainsColumn(persistentClass.getUnjoinedPropertyIterator(), column)) {
                // The index column is not mapped to an associated entity property so we can
                // safely make the index column nullable.
                column.setNullable(true);
            }
        }
    }
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) Selectable(org.hibernate.mapping.Selectable) Ejb3Column(org.hibernate.cfg.Ejb3Column) MapKeyColumn(javax.persistence.MapKeyColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) Column(org.hibernate.mapping.Column) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn) SimpleValue(org.hibernate.mapping.SimpleValue) DependantValue(org.hibernate.mapping.DependantValue) Value(org.hibernate.mapping.Value) MapKeyColumn(javax.persistence.MapKeyColumn) OneToMany(org.hibernate.mapping.OneToMany) Map(java.util.Map) HashMap(java.util.HashMap) PersistentClass(org.hibernate.mapping.PersistentClass)

Aggregations

Selectable (org.hibernate.mapping.Selectable)17 Column (org.hibernate.mapping.Column)14 Property (org.hibernate.mapping.Property)5 ConstraintDescriptor (javax.validation.metadata.ConstraintDescriptor)4 SimpleValue (org.hibernate.mapping.SimpleValue)4 Iterator (java.util.Iterator)3 AnnotationException (org.hibernate.AnnotationException)3 Component (org.hibernate.mapping.Component)3 Value (org.hibernate.mapping.Value)3 MapKeyColumn (javax.persistence.MapKeyColumn)2 MapKeyJoinColumn (javax.persistence.MapKeyJoinColumn)2 AssertionFailure (org.hibernate.AssertionFailure)2 Ejb3Column (org.hibernate.cfg.Ejb3Column)2 Ejb3JoinColumn (org.hibernate.cfg.Ejb3JoinColumn)2 DependantValue (org.hibernate.mapping.DependantValue)2 OneToMany (org.hibernate.mapping.OneToMany)2 PersistentClass (org.hibernate.mapping.PersistentClass)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Random (java.util.Random)1