Example usage for org.hibernate.mapping PersistentClass hasIdentifierProperty

List of usage examples for org.hibernate.mapping PersistentClass hasIdentifierProperty

Introduction

In this page you can find the example usage for org.hibernate.mapping PersistentClass hasIdentifierProperty.

Prototype

public abstract boolean hasIdentifierProperty();

Source Link

Usage

From source file:com.mysema.query.jpa.codegen.HibernateDomainExporter.java

License:Apache License

private void collectTypes() throws IOException, XMLStreamException, ClassNotFoundException, SecurityException,
        NoSuchMethodException {//  w w w.j  a v  a2  s.  c o  m
    // super classes
    Iterator<?> superClassMappings = configuration.getMappedSuperclassMappings();
    while (superClassMappings.hasNext()) {
        MappedSuperclass msc = (MappedSuperclass) superClassMappings.next();
        EntityType entityType = createSuperType(msc.getMappedClass());
        if (msc.getDeclaredIdentifierProperty() != null) {
            handleProperty(entityType, msc.getMappedClass(), msc.getDeclaredIdentifierProperty());
        }
        Iterator<?> properties = msc.getDeclaredPropertyIterator();
        while (properties.hasNext()) {
            handleProperty(entityType, msc.getMappedClass(),
                    (org.hibernate.mapping.Property) properties.next());
        }
    }

    // entity classes
    Iterator<?> classMappings = configuration.getClassMappings();
    while (classMappings.hasNext()) {
        PersistentClass pc = (PersistentClass) classMappings.next();
        EntityType entityType = createEntityType(pc.getMappedClass());
        if (pc.getDeclaredIdentifierProperty() != null) {
            handleProperty(entityType, pc.getMappedClass(), pc.getDeclaredIdentifierProperty());
        } else if (!pc.isInherited() && pc.hasIdentifierProperty()) {
            logger.info(entityType.toString() + pc.getIdentifierProperty());
            handleProperty(entityType, pc.getMappedClass(), pc.getIdentifierProperty());
        } else if (pc.getIdentifier() != null) {
            KeyValue identifier = pc.getIdentifier();
            if (identifier instanceof Component) {
                Component component = (Component) identifier;
                Iterator<?> properties = component.getPropertyIterator();
                if (component.isEmbedded()) {
                    while (properties.hasNext()) {
                        handleProperty(entityType, pc.getMappedClass(),
                                (org.hibernate.mapping.Property) properties.next());
                    }
                } else {
                    String name = component.getNodeName();
                    Class<?> clazz = component.getType().getReturnedClass();
                    Type propertyType = getType(pc.getMappedClass(), clazz, name);
                    AnnotatedElement annotated = getAnnotatedElement(pc.getMappedClass(), name);
                    Property property = createProperty(entityType, name, propertyType, annotated);
                    entityType.addProperty(property);
                    // handle component properties
                    EntityType embeddedType = createEmbeddableType(propertyType);
                    while (properties.hasNext()) {
                        handleProperty(embeddedType, clazz, (org.hibernate.mapping.Property) properties.next());
                    }
                }
            }
            // TODO handle other KeyValue subclasses such as Any, DependentValue and ToOne
        }
        Iterator<?> properties = pc.getDeclaredPropertyIterator();
        while (properties.hasNext()) {
            handleProperty(entityType, pc.getMappedClass(), (org.hibernate.mapping.Property) properties.next());
        }
    }

    // go through supertypes
    Set<Supertype> additions = Sets.newHashSet();
    for (Map.Entry<String, EntityType> entry : allTypes.entrySet()) {
        EntityType entityType = entry.getValue();
        if (entityType.getSuperType() != null
                && !allTypes.containsKey(entityType.getSuperType().getType().getFullName())) {
            additions.add(entityType.getSuperType());
        }
    }

    for (Supertype type : additions) {
        type.setEntityType(createEntityType(type.getType(), superTypes));
    }
}

From source file:com.netspective.tool.hibernate.document.diagram.HibernateDiagramGenerator.java

License:Open Source License

public HibernateDiagramGenerator(final Configuration configuration,
        final GraphvizDiagramGenerator graphvizDiagramGenerator,
        final HibernateDiagramGeneratorFilter schemaDiagramFilter) throws HibernateDiagramGeneratorException {
    this.configuration = configuration;
    this.graphvizDiagramGenerator = graphvizDiagramGenerator;
    this.diagramFilter = schemaDiagramFilter;

    // the following was copied from org.hibernate.cfg.Configuration.buildMapping() because buildMapping() was private
    this.mapping = new Mapping() {
        /**/*from w ww  .j  av a  2s.c  o m*/
         * Returns the identifier type of a mapped class
         */
        public Type getIdentifierType(String persistentClass) throws MappingException {
            final PersistentClass pc = configuration.getClassMapping(persistentClass);
            if (pc == null)
                throw new MappingException("persistent class not known: " + persistentClass);
            return pc.getIdentifier().getType();
        }

        public String getIdentifierPropertyName(String persistentClass) throws MappingException {
            final PersistentClass pc = configuration.getClassMapping(persistentClass);
            if (pc == null)
                throw new MappingException("persistent class not known: " + persistentClass);
            if (!pc.hasIdentifierProperty())
                return null;
            return pc.getIdentifierProperty().getName();
        }

        public Type getPropertyType(String persistentClass, String propertyName) throws MappingException {
            final PersistentClass pc = configuration.getClassMapping(persistentClass);
            if (pc == null)
                throw new MappingException("persistent class not known: " + persistentClass);
            Property prop = pc.getProperty(propertyName);
            if (prop == null)
                throw new MappingException("property not known: " + persistentClass + '.' + propertyName);
            return prop.getType();
        }
    };

    String dialectName = configuration.getProperty(Environment.DIALECT);
    if (dialectName == null)
        dialectName = org.hibernate.dialect.GenericDialect.class.getName();

    try {
        final Class cls = Class.forName(dialectName);
        this.dialect = (Dialect) cls.newInstance();
    } catch (Exception e) {
        throw new HibernateDiagramGeneratorException(e);
    }

    for (final Iterator classes = configuration.getClassMappings(); classes.hasNext();) {
        final PersistentClass pclass = (PersistentClass) classes.next();
        final Table table = (Table) pclass.getTable();

        tableToClassMap.put(table, pclass);
    }
}

From source file:com.querydsl.jpa.codegen.HibernateDomainExporter.java

License:Apache License

@Override
protected void collectTypes()
        throws IOException, XMLStreamException, ClassNotFoundException, NoSuchMethodException {
    // super classes
    Iterator<?> superClassMappings = configuration.getMappedSuperclassMappings();
    while (superClassMappings.hasNext()) {
        MappedSuperclass msc = (MappedSuperclass) superClassMappings.next();
        EntityType entityType = createSuperType(msc.getMappedClass());
        if (msc.getDeclaredIdentifierProperty() != null) {
            handleProperty(entityType, msc.getMappedClass(), msc.getDeclaredIdentifierProperty());
        }//www  .  j a va2 s .c o  m
        Iterator<?> properties = msc.getDeclaredPropertyIterator();
        while (properties.hasNext()) {
            handleProperty(entityType, msc.getMappedClass(),
                    (org.hibernate.mapping.Property) properties.next());
        }
    }

    // entity classes
    Iterator<?> classMappings = configuration.getClassMappings();
    while (classMappings.hasNext()) {
        PersistentClass pc = (PersistentClass) classMappings.next();
        EntityType entityType = createEntityType(pc.getMappedClass());
        if (pc.getDeclaredIdentifierProperty() != null) {
            handleProperty(entityType, pc.getMappedClass(), pc.getDeclaredIdentifierProperty());
        } else if (!pc.isInherited() && pc.hasIdentifierProperty()) {
            logger.info(entityType.toString() + pc.getIdentifierProperty());
            handleProperty(entityType, pc.getMappedClass(), pc.getIdentifierProperty());
        } else if (pc.getIdentifier() != null) {
            KeyValue identifier = pc.getIdentifier();
            if (identifier instanceof Component) {
                Component component = (Component) identifier;
                Iterator<?> properties = component.getPropertyIterator();
                if (component.isEmbedded()) {
                    while (properties.hasNext()) {
                        handleProperty(entityType, pc.getMappedClass(),
                                (org.hibernate.mapping.Property) properties.next());
                    }
                } else {
                    String name = component.getNodeName();
                    Class<?> clazz = component.getType().getReturnedClass();
                    Type propertyType = getType(pc.getMappedClass(), clazz, name);
                    AnnotatedElement annotated = getAnnotatedElement(pc.getMappedClass(), name);
                    Property property = createProperty(entityType, name, propertyType, annotated);
                    entityType.addProperty(property);
                    // handle component properties
                    EntityType embeddedType = createEmbeddableType(propertyType);
                    while (properties.hasNext()) {
                        handleProperty(embeddedType, clazz, (org.hibernate.mapping.Property) properties.next());
                    }
                }
            }
            // TODO handle other KeyValue subclasses such as Any, DependentValue and ToOne
        }
        Iterator<?> properties = pc.getDeclaredPropertyIterator();
        while (properties.hasNext()) {
            handleProperty(entityType, pc.getMappedClass(), (org.hibernate.mapping.Property) properties.next());
        }
    }
}

From source file:com.zutubi.pulse.master.hibernate.MutableConfiguration.java

License:Apache License

public Mapping getMapping() {
    return new Mapping() {
        public IdentifierGeneratorFactory getIdentifierGeneratorFactory() {
            return null;
        }/* w w  w  . j ava  2s . c  om*/

        public Type getIdentifierType(String persistentClass) throws MappingException {
            PersistentClass pc = classes.get(persistentClass);
            if (pc == null) {
                throw new MappingException(I18N.format("unknown.persistent.class", persistentClass));
            }
            return pc.getIdentifier().getType();
        }

        public String getIdentifierPropertyName(String persistentClass) throws MappingException {
            final PersistentClass pc = classes.get(persistentClass);
            if (pc == null) {
                throw new MappingException(I18N.format("unknown.persistent.class", persistentClass));
            }
            if (!pc.hasIdentifierProperty())
                return null;
            return pc.getIdentifierProperty().getName();
        }

        public Type getReferencedPropertyType(String persistentClass, String propertyName)
                throws MappingException {
            final PersistentClass pc = classes.get(persistentClass);
            if (pc == null) {
                throw new MappingException(I18N.format("unknown.persistent.class", persistentClass));
            }
            Property prop = pc.getReferencedProperty(propertyName);
            if (prop == null) {
                throw new MappingException(
                        I18N.format("unknown.persistent.class.property", persistentClass + '.' + propertyName));
            }
            return prop.getType();
        }
    };
}

From source file:com.zutubi.pulse.master.transfer.jdbc.HibernateMapping.java

License:Apache License

public String getIdentifierPropertyName(String persistentClass) throws MappingException {
    final PersistentClass pc = configuration.getClassMapping(persistentClass);
    if (pc == null) {
        throw new MappingException("persistent class not known: " + persistentClass);
    }/*  ww w  .  j  a v a2 s. c om*/
    if (!pc.hasIdentifierProperty())
        return null;
    return pc.getIdentifierProperty().getName();
}

From source file:gov.nih.nci.caarray.dao.AbstractCaArrayDaoImpl.java

License:BSD License

Criterion createExample(Object entity, MatchMode matchMode, boolean excludeNulls, boolean excludeZeroes,
        Collection<String> excludeProperties) {
    final Example example = Example.create(entity).enableLike(matchMode).ignoreCase();
    if (excludeZeroes) {
        example.excludeZeroes();//from  w  w w. j  a  v  a 2  s. c o  m
    } else if (!excludeNulls) {
        example.excludeNone();
    }
    for (final String property : excludeProperties) {
        example.excludeProperty(property);
    }

    // ID property is not handled by Example, so we have to special case it
    final PersistentClass pclass = getClassMapping(entity.getClass());
    Object idVal = null;
    if (pclass != null && pclass.hasIdentifierProperty()) {
        try {
            idVal = PropertyUtils.getProperty(entity, pclass.getIdentifierProperty().getName());
        } catch (final Exception e) {
            LOG.warn("Could not retrieve identifier value in a by example query, ignoring it", e);
        }
    }
    if (idVal == null) {
        return example;
    } else {
        return Restrictions.and(Restrictions.idEq(idVal), example);
    }
}

From source file:org.sns.tool.hibernate.analyzer.HibernateAnalysis.java

License:Open Source License

public HibernateAnalysis(final Configuration configuration, final List<HibernateAnalysisRule> analyzers) {
    this.configuration = configuration;
    this.analysisRules = analyzers;

    // the following was copied from org.hibernate.cfg.Configuration.buildMapping() because buildMapping() was private
    this.mapping = new Mapping() {
        /**//w  w  w.ja v  a 2 s .  c  om
         * Returns the identifier type of a mapped class
         */
        public Type getIdentifierType(String persistentClass) throws MappingException {
            final PersistentClass pc = configuration.getClassMapping(persistentClass);
            if (pc == null)
                throw new MappingException("persistent class not known: " + persistentClass);
            return pc.getIdentifier().getType();
        }

        public String getIdentifierPropertyName(String persistentClass) throws MappingException {
            final PersistentClass pc = configuration.getClassMapping(persistentClass);
            if (pc == null)
                throw new MappingException("persistent class not known: " + persistentClass);
            if (!pc.hasIdentifierProperty())
                return null;
            return pc.getIdentifierProperty().getName();
        }

        public Type getPropertyType(String persistentClass, String propertyName) throws MappingException {
            final PersistentClass pc = configuration.getClassMapping(persistentClass);
            if (pc == null)
                throw new MappingException("persistent class not known: " + persistentClass);
            Property prop = pc.getProperty(propertyName);
            if (prop == null)
                throw new MappingException("property not known: " + persistentClass + '.' + propertyName);
            return prop.getType();
        }
    };

    String dialectName = configuration.getProperty(Environment.DIALECT);
    if (dialectName == null)
        dialectName = org.hibernate.dialect.GenericDialect.class.getName();

    try {
        final Class cls = Class.forName(dialectName);
        this.dialect = (Dialect) cls.newInstance();
    } catch (Exception e) {
        throw new HibernateDiagramGeneratorException(e);
    }

    for (final Iterator classes = configuration.getClassMappings(); classes.hasNext();) {
        final PersistentClass pclass = (PersistentClass) classes.next();
        final Table table = (Table) pclass.getTable();

        tableToClassMap.put(table, pclass);
    }
}

From source file:org.sns.tool.hibernate.struct.impl.DefaultTableStructure.java

License:Open Source License

public DefaultTableStructure(final Configuration configuration, final TableStructureRules rules) {
    this.configuration = configuration;
    this.configuration.buildMappings();
    this.rules = rules;

    for (final Iterator classes = configuration.getClassMappings(); classes.hasNext();) {
        final PersistentClass pclass = (PersistentClass) classes.next();
        final Table table = (Table) pclass.getTable();

        boolean isChildTable = false;
        for (final Iterator fKeys = table.getForeignKeyIterator(); fKeys.hasNext();) {
            final ForeignKey foreignKey = (ForeignKey) fKeys.next();
            if (rules.isParentRelationship(this, foreignKey)) {
                isChildTable = true;//from   ww w .  j a v a2 s . c  om
                break;
            }
        }

        if (!isChildTable) {
            final TableStructureNode topLevelNode = createNode(pclass, table, null, 0);
            topLevelTableNodes.add(topLevelNode);
            categorize(topLevelNode);
        }
    }

    for (final Iterator classes = configuration.getClassMappings(); classes.hasNext();) {
        final PersistentClass pclass = (PersistentClass) classes.next();
        final Table table = (Table) pclass.getTable();

        tableToClassMap.put(table, pclass);
    }

    // the following was copied from org.hibernate.cfg.Configuration.buildMapping() because buildMapping() was private
    this.mapping = new Mapping() {
        public Type getReferencedPropertyType(String string, String string1) throws MappingException {
            return null;
        }

        /**
         * Returns the identifier type of a mapped class
         */
        public Type getIdentifierType(String persistentClass) throws MappingException {
            final PersistentClass pc = configuration.getClassMapping(persistentClass);
            if (pc == null)
                throw new MappingException("persistent class not known: " + persistentClass);
            return pc.getIdentifier().getType();
        }

        public String getIdentifierPropertyName(String persistentClass) throws MappingException {
            final PersistentClass pc = configuration.getClassMapping(persistentClass);
            if (pc == null)
                throw new MappingException("persistent class not known: " + persistentClass);
            if (!pc.hasIdentifierProperty())
                return null;
            return pc.getIdentifierProperty().getName();
        }

        public Type getPropertyType(String persistentClass, String propertyName) throws MappingException {
            final PersistentClass pc = configuration.getClassMapping(persistentClass);
            if (pc == null)
                throw new MappingException("persistent class not known: " + persistentClass);
            Property prop = pc.getProperty(propertyName);
            if (prop == null)
                throw new MappingException("property not known: " + persistentClass + '.' + propertyName);
            return prop.getType();
        }
    };

    String dialectName = configuration.getProperty(Environment.DIALECT);
    if (dialectName == null)
        dialectName = org.hibernate.dialect.GenericDialect.class.getName();

    try {
        final Class cls = Class.forName(dialectName);
        this.dialect = (Dialect) cls.newInstance();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    resolveDependencies();
}

From source file:org.teiid.spring.autoconfigure.SchemaBuilderUtility.java

License:Apache License

private static Mapping buildMapping(final Metadata metadata) {
    return new Mapping() {
        /**/*from  w  w w.  j  ava  2 s.c o  m*/
         * Returns the identifier type of a mapped class
         */
        @Override
        public Type getIdentifierType(String persistentClass) throws MappingException {
            final PersistentClass pc = metadata.getEntityBinding(persistentClass);
            if (pc == null) {
                throw new MappingException("persistent class not known: " + persistentClass);
            }
            return pc.getIdentifier().getType();
        }

        @Override
        public String getIdentifierPropertyName(String persistentClass) throws MappingException {
            final PersistentClass pc = metadata.getEntityBinding(persistentClass);
            if (pc == null) {
                throw new MappingException("persistent class not known: " + persistentClass);
            }
            if (!pc.hasIdentifierProperty()) {
                return null;
            }
            return pc.getIdentifierProperty().getName();
        }

        @Override
        public Type getReferencedPropertyType(String persistentClass, String propertyName)
                throws MappingException {
            final PersistentClass pc = metadata.getEntityBinding(persistentClass);
            if (pc == null) {
                throw new MappingException("persistent class not known: " + persistentClass);
            }
            Property prop = pc.getProperty(propertyName);
            if (prop == null) {
                throw new MappingException("property not known: " + persistentClass + '.' + propertyName);
            }
            return prop.getType();
        }

        @Override
        public IdentifierGeneratorFactory getIdentifierGeneratorFactory() {
            return null;
        }
    };
}