Search in sources :

Example 1 with DiscriminatorSource

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

the class EntityHierarchySourceImpl method interpretDiscriminatorSource.

private static DiscriminatorSource interpretDiscriminatorSource(final RootEntitySourceImpl rootEntitySource) {
    final JaxbHbmEntityDiscriminatorType jaxbDiscriminatorMapping = rootEntitySource.jaxbEntityMapping().getDiscriminator();
    if (jaxbDiscriminatorMapping == null) {
        return null;
    }
    final RelationalValueSource relationalValueSource = RelationalValueSourceHelper.buildValueSource(rootEntitySource.sourceMappingDocument(), null, new RelationalValueSourceHelper.AbstractColumnsAndFormulasSource() {

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

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

        @Override
        public SizeSource getSizeSource() {
            return Helper.interpretSizeSource(jaxbDiscriminatorMapping.getLength(), (Integer) null, null);
        }

        @Override
        public String getFormulaAttribute() {
            return jaxbDiscriminatorMapping.getFormulaAttribute();
        }

        @Override
        public String getColumnAttribute() {
            return jaxbDiscriminatorMapping.getColumnAttribute();
        }

        private List columnOrFormulas;

        @Override
        public List getColumnOrFormulaElements() {
            if (columnOrFormulas == null) {
                if (jaxbDiscriminatorMapping.getColumn() != null) {
                    if (jaxbDiscriminatorMapping.getFormula() != null) {
                        throw new MappingException(String.format(Locale.ENGLISH, "discriminator mapping [%s] named both <column/> and <formula/>, but only one or other allowed", rootEntitySource.getEntityNamingSource().getEntityName()), rootEntitySource.sourceMappingDocument().getOrigin());
                    } else {
                        columnOrFormulas = Collections.singletonList(jaxbDiscriminatorMapping.getColumn());
                    }
                } else {
                    if (jaxbDiscriminatorMapping.getFormula() != null) {
                        columnOrFormulas = Collections.singletonList(jaxbDiscriminatorMapping.getFormula());
                    } else {
                        columnOrFormulas = Collections.emptyList();
                    }
                }
            }
            return columnOrFormulas;
        }

        @Override
        public Boolean isNullable() {
            return !jaxbDiscriminatorMapping.isNotNull();
        }
    });
    return new DiscriminatorSource() {

        @Override
        public EntityNaming getEntityNaming() {
            return rootEntitySource.getEntityNamingSource();
        }

        @Override
        public MetadataBuildingContext getBuildingContext() {
            return rootEntitySource.metadataBuildingContext();
        }

        @Override
        public RelationalValueSource getDiscriminatorRelationalValueSource() {
            return relationalValueSource;
        }

        @Override
        public String getExplicitHibernateTypeName() {
            return jaxbDiscriminatorMapping.getType();
        }

        @Override
        public boolean isForced() {
            return jaxbDiscriminatorMapping.isForce();
        }

        @Override
        public boolean isInserted() {
            return jaxbDiscriminatorMapping.isInsert();
        }
    };
}
Also used : SizeSource(org.hibernate.boot.model.source.spi.SizeSource) JaxbHbmEntityDiscriminatorType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmEntityDiscriminatorType) RelationalValueSource(org.hibernate.boot.model.source.spi.RelationalValueSource) DiscriminatorSource(org.hibernate.boot.model.source.spi.DiscriminatorSource) MappingException(org.hibernate.boot.MappingException) List(java.util.List)

Aggregations

List (java.util.List)1 MappingException (org.hibernate.boot.MappingException)1 JaxbHbmEntityDiscriminatorType (org.hibernate.boot.jaxb.hbm.spi.JaxbHbmEntityDiscriminatorType)1 DiscriminatorSource (org.hibernate.boot.model.source.spi.DiscriminatorSource)1 RelationalValueSource (org.hibernate.boot.model.source.spi.RelationalValueSource)1 SizeSource (org.hibernate.boot.model.source.spi.SizeSource)1