Example usage for org.hibernate.mapping PersistentClass getIdentifierProperty

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

Introduction

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

Prototype

public abstract Property getIdentifierProperty();

Source Link

Usage

From source file:com.fiveamsolutions.nci.commons.audit.DefaultProcessor.java

License:Open Source License

/**
 * @param entity an entity.//from w  ww. j a v a 2  s  . co  m
 * @return the id of an entity.
 */
protected Long getId(Object entity) {
    // Note: Only Long primary keys are supported.
    @SuppressWarnings(RAWTYPES)
    Class c = ProxyUtils.unEnhanceClass(entity.getClass());
    PersistentClass pc = hibernateHelper.getConfiguration().getClassMapping(c.getName());
    if (pc == null) {
        throw new IllegalArgumentException("not persistent");
    }
    pc.getIdentifierProperty().getGetter(c).get(entity);
    return (Long) pc.getIdentifierProperty().getGetter(c).get(entity);
}

From source file:com.github.shyiko.rook.target.hibernate4.fulltextindex.PrimaryKey.java

License:Apache License

public PrimaryKey(PersistentClass persistentClass) {
    this.entityClass = persistentClass.getMappedClass();
    KeyValue keyValue = persistentClass.getKey();
    Table table = persistentClass.getTable();
    Map<String, Integer> columnIndexByNameMap = getColumnIndexByNameMap(table);
    KeyColumn[] positionWithinRow = new KeyColumn[keyValue.getColumnSpan()];
    int index = 0;
    if (keyValue instanceof Component) {
        Iterator propertyIterator = ((Component) keyValue).getPropertyIterator();
        while (propertyIterator.hasNext()) {
            Property property = (Property) propertyIterator.next();
            String columnName = ((Column) property.getColumnIterator().next()).getName();
            positionWithinRow[index++] = new KeyColumn(property.getName(),
                    columnIndexByNameMap.get(columnName));
        }//  w  ww  .j  a  v  a 2  s.c  om
    } else {
        Iterator columnIterator = keyValue.getColumnIterator();
        while (columnIterator.hasNext()) {
            String columnName = ((Column) columnIterator.next()).getName();
            positionWithinRow[index++] = new KeyColumn(columnName, columnIndexByNameMap.get(columnName));
        }
    }
    if (positionWithinRow.length == 0) {
        throw new IllegalStateException("Unable to determine PK for " + table.getName());
    }
    Property identifierProperty = persistentClass.getIdentifierProperty();
    this.getter = identifierProperty.getGetter(this.entityClass);
    this.positionWithinRow = positionWithinRow;

}

From source file:com.googlecode.hibernate.audit.listener.AuditSessionFactoryObserver.java

License:Open Source License

private AuditType initializeEntityAuditType(Session session, String entityName, boolean initializeProperties) {
    PersistentClass classMapping = auditConfiguration.getMetadata().getEntityBinding(entityName);
    String auditTypeClassName = auditConfiguration.getExtensionManager().getAuditableInformationProvider()
            .getAuditTypeClassName(auditConfiguration.getMetadata(), entityName);

    AuditType auditType = HibernateAudit.getAuditType(session, auditTypeClassName);
    if (auditType == null) {
        auditType = new AuditType();
        auditType.setClassName(auditTypeClassName);
        auditType.setLabel(entityName);/*from  www  . j  a  v  a 2  s.c om*/
        auditType.setType(AuditType.ENTITY_TYPE);

        session.save(auditType);
        updateMetaModel(session);
    }

    if (initializeProperties) {
        Property identifierProperty = classMapping.getIdentifierProperty();
        if (identifierProperty != null) {
            initializeAuditField(session, auditTypeClassName, auditType, identifierProperty.getName(),
                    identifierProperty.getType());
        }

        for (Iterator propertyIterator = classMapping.getPropertyClosureIterator(); propertyIterator
                .hasNext();) {
            Property property = (Property) propertyIterator.next();
            initializeAuditField(session, auditTypeClassName, auditType, property.getName(),
                    property.getType());
        }
    }
    return auditType;
}

From source file:com.manydesigns.portofino.persistence.hibernate.HibernateConfig.java

License:Open Source License

private Property getRefProperty(PersistentClass clazzOne, String propertyName) {
    Property refProp;//www.j ava  2 s .  co m
    //TODO alessio ha senso questo automatismo?
    if (null != clazzOne.getIdentifierProperty()) {
        refProp = clazzOne.getIdentifierProperty();
    } else if (null != clazzOne.getIdentifier()) {
        refProp = ((Component) clazzOne.getIdentifier()).getProperty(propertyName);
    } else {
        refProp = clazzOne.getProperty(propertyName);
    }
    return refProp;
}

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

License:Apache License

private void collectTypes() throws IOException, XMLStreamException, ClassNotFoundException, SecurityException,
        NoSuchMethodException {/*from  ww 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() {
        /**//  ww  w. j  a 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: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());
        }/* ww  w  .  j a va 2 s .com*/
        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.vecna.maven.hibernate.HibernateDocMojo.java

License:Apache License

/**
 * Populate table/column comments in a Hibernate model from javadocs
 *///from  ww w  .j a  v  a  2  s.co  m
private void populateCommentsFromJavadocs(Configuration configuration, JavaDocBuilder javaDocs) {
    Iterator<PersistentClass> mappedClasses = configuration.getClassMappings();
    while (mappedClasses.hasNext()) {
        PersistentClass mappedClass = mappedClasses.next();

        Table table = mappedClass.getTable();
        JavaClass javaClass = javaDocs.getClassByName(mappedClass.getClassName());

        if (javaClass != null) {
            if (table != null) {
                String comment = javaClass.getComment();

                if (mappedClass.getDiscriminator() != null) {
                    String newComment = "Discriminator '" + mappedClass.getDiscriminatorValue() + "': "
                            + comment;
                    if (table.getComment() != null) {
                        newComment = table.getComment() + "<br><br>" + newComment;
                    }
                    table.setComment(newComment);
                    @SuppressWarnings("unchecked")
                    Iterator<Column> discriminatorColumns = mappedClass.getDiscriminator().getColumnIterator();
                    setComment("discriminator - see table comment", discriminatorColumns);
                } else {
                    table.setComment(comment);
                }
            }

            @SuppressWarnings("unchecked")
            Iterator<Property> propertyIterator = mappedClass.getPropertyIterator();
            processProperties(propertyIterator, mappedClass.getMappedClass(), javaDocs, null);
        }

        if (mappedClass.getIdentifierProperty() != null) {
            setComment("Primary key", mappedClass.getIdentifierProperty());
        }
    }
}

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

License:Apache License

public Mapping getMapping() {
    return new Mapping() {
        public IdentifierGeneratorFactory getIdentifierGeneratorFactory() {
            return null;
        }//from   w ww  .j  a v  a2 s  .c o  m

        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);
    }/*from ww  w .java 2s  .  c  o m*/
    if (!pc.hasIdentifierProperty())
        return null;
    return pc.getIdentifierProperty().getName();
}