Example usage for org.hibernate.metadata ClassMetadata getVersionProperty

List of usage examples for org.hibernate.metadata ClassMetadata getVersionProperty

Introduction

In this page you can find the example usage for org.hibernate.metadata ClassMetadata getVersionProperty.

Prototype

int getVersionProperty();

Source Link

Document

Get the index of the version property

Usage

From source file:org.codehaus.groovy.grails.orm.hibernate.AbstractGrailsHibernateDomainClass.java

License:Apache License

/**
 * Contructor to be used by all child classes to create a new instance
 * and get the name right.//from   w ww. j a va  2s.  com
 *
 * @param clazz          the Grails class
 * @param sessionFactory The Hibernate SessionFactory instance
 * @param sessionFactoryName
 * @param application
 * @param metaData       The ClassMetaData for this class retrieved from the SF
 */
public AbstractGrailsHibernateDomainClass(Class<?> clazz, SessionFactory sessionFactory,
        String sessionFactoryName, GrailsApplication application, ClassMetadata metaData) {
    super(clazz, "");
    this.application = application;
    this.sessionFactory = sessionFactory;
    this.sessionFactoryName = sessionFactoryName;

    new StandardAnnotationMetadata(clazz);
    String ident = metaData.getIdentifierPropertyName();
    if (ident != null) {
        Class<?> identType = getPropertyType(ident);
        identifier = new GrailsHibernateDomainClassProperty(this, ident);
        identifier.setIdentity(true);
        identifier.setType(identType);
        propertyMap.put(ident, identifier);
    }

    // configure the version property
    final int versionIndex = metaData.getVersionProperty();
    String versionPropertyName = null;
    if (versionIndex > -1) {
        versionPropertyName = metaData.getPropertyNames()[versionIndex];
        version = new GrailsHibernateDomainClassProperty(this, versionPropertyName);
        version.setType(getPropertyType(versionPropertyName));
    }

    // configure remaining properties
    String[] propertyNames = metaData.getPropertyNames();
    for (String propertyName : propertyNames) {
        if (!propertyName.equals(ident)
                && !(versionPropertyName != null && propertyName.equals(versionPropertyName))) {

            PropertyDescriptor pd = GrailsClassUtils.getProperty(clazz, propertyName);
            if (pd == null)
                continue;

            GrailsHibernateDomainClassProperty prop = new GrailsHibernateDomainClassProperty(this,
                    propertyName);
            prop.setType(getPropertyType(propertyName));
            Type hibernateType = metaData.getPropertyType(propertyName);

            // if its an association type
            if (hibernateType.isAssociationType()) {
                prop.setAssociation(true);
                // get the associated type from the session factory and set it on the property
                AssociationType assType = (AssociationType) hibernateType;
                if (assType instanceof AnyType) {
                    continue;
                }
                setRelatedClassType(prop, assType, hibernateType);
                // configure type of relationship
                if (hibernateType.isCollectionType()) {
                    prop.setOneToMany(true);
                } else if (hibernateType.isEntityType()) {
                    prop.setManyToOne(true);
                    // might not really be true, but for our purposes this is ok
                    prop.setOneToOne(true);
                }
            }
            propertyMap.put(propertyName, prop);
        }
    }

    properties = propertyMap.values().toArray(new GrailsDomainClassProperty[propertyMap.size()]);
    // process the constraints
    evaluateConstraints();
}

From source file:org.codehaus.groovy.grails.orm.hibernate.GrailsHibernateDomainClass.java

License:Apache License

/**
 * Contructor to be used by all child classes to create a new instance
 * and get the name right.// w w  w. jav  a 2  s  . com
 *
 * @param clazz          the Grails class
 * @param sessionFactory The Hibernate SessionFactory instance
 * @param metaData       The ClassMetaData for this class retrieved from the SF
 */
public GrailsHibernateDomainClass(Class<?> clazz, SessionFactory sessionFactory, GrailsApplication application,
        ClassMetadata metaData) {
    super(clazz, "");
    this.application = application;

    new StandardAnnotationMetadata(clazz);
    String ident = metaData.getIdentifierPropertyName();
    if (ident != null) {
        Class<?> identType = getPropertyType(ident);
        identifier = new GrailsHibernateDomainClassProperty(this, ident);
        identifier.setIdentity(true);
        identifier.setType(identType);
        propertyMap.put(ident, identifier);
    }

    // configure the version property
    final int versionIndex = metaData.getVersionProperty();
    String versionPropertyName = null;
    if (versionIndex > -1) {
        versionPropertyName = metaData.getPropertyNames()[versionIndex];
        version = new GrailsHibernateDomainClassProperty(this, versionPropertyName);
        version.setType(getPropertyType(versionPropertyName));
    }

    // configure remaining properties
    String[] propertyNames = metaData.getPropertyNames();
    for (String propertyName : propertyNames) {
        if (!propertyName.equals(ident)
                && !(versionPropertyName != null && propertyName.equals(versionPropertyName))) {
            GrailsHibernateDomainClassProperty prop = new GrailsHibernateDomainClassProperty(this,
                    propertyName);
            prop.setType(getPropertyType(propertyName));
            Type hibernateType = metaData.getPropertyType(propertyName);

            // if its an association type
            if (hibernateType.isAssociationType()) {
                prop.setAssociation(true);
                // get the associated type from the session factory and set it on the property
                AssociationType assType = (AssociationType) hibernateType;
                if (assType instanceof AnyType) {
                    continue;
                }
                try {
                    String associatedEntity = assType
                            .getAssociatedEntityName((SessionFactoryImplementor) sessionFactory);
                    ClassMetadata associatedMetaData = sessionFactory.getClassMetadata(associatedEntity);
                    prop.setRelatedClassType(associatedMetaData.getMappedClass(EntityMode.POJO));
                } catch (MappingException me) {
                    // other side must be a value object
                    if (hibernateType.isCollectionType()) {
                        prop.setRelatedClassType(Collection.class);
                    }
                }
                // configure type of relationship
                if (hibernateType.isCollectionType()) {
                    prop.setOneToMany(true);
                } else if (hibernateType.isEntityType()) {
                    prop.setManyToOne(true);
                    // might not really be true, but for our purposes this is ok
                    prop.setOneToOne(true);
                }
            }
            propertyMap.put(propertyName, prop);
        }
    }

    properties = propertyMap.values().toArray(new GrailsDomainClassProperty[propertyMap.size()]);
    // process the constraints
    evaluateConstraints();
}

From source file:org.web4thejob.orm.EntityMetadataImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public EntityMetadataImpl(ClassMetadata classMetadata) {
    this.classMetadata = classMetadata;
    this.persistentClass = ContextUtil.getBean(HibernateConfiguration.class).getConfiguration()
            .getClassMapping(classMetadata.getEntityName());
    entityType = getEntityType(classMetadata.getEntityName());
    identifier = classMetadata.getIdentifierPropertyName();
    identifierType = classMetadata.getIdentifierType();
    versioned = classMetadata.getVersionProperty() >= 0;
    if (MetaUtil.hasMetaAttribute(this.persistentClass, META_FRIENDLY_NAME)) {
        friendlyName = MetaUtil.getMetaAttribute(this.persistentClass, META_FRIENDLY_NAME);
    } else {//from w  ww .  ja  v  a 2s.co m
        friendlyName = classMetadata.getEntityName();
    }
    if (MetaUtil.hasMetaAttribute(this.persistentClass, META_TABLE_SUBSET)) {
        tableSubset = Boolean.parseBoolean(MetaUtil.getMetaAttribute(this.persistentClass, META_TABLE_SUBSET));
    } else {
        tableSubset = false;
    }
    if (MetaUtil.hasMetaAttribute(this.persistentClass, META_TABLE_CACHED)) {
        cached = Boolean.parseBoolean(MetaUtil.getMetaAttribute(this.persistentClass, META_TABLE_CACHED));
    } else {
        cached = false;
    }

    boolean isAbstract = persistentClass.isAbstract() != null ? persistentClass.isAbstract() : false;
    if (MetaUtil.hasMetaAttribute(this.persistentClass, META_DENY_ADDNEW)) {
        denyAddNew = isAbstract
                || Boolean.parseBoolean(MetaUtil.getMetaAttribute(this.persistentClass, META_DENY_ADDNEW));
    } else {
        denyAddNew = isAbstract;
    }

    if (MetaUtil.hasMetaAttribute(this.persistentClass, META_DENY_UPDATE)) {
        denyUpdate = Boolean.parseBoolean(MetaUtil.getMetaAttribute(this.persistentClass, META_DENY_UPDATE));
    } else {
        denyUpdate = false;
    }

    if (MetaUtil.hasMetaAttribute(this.persistentClass, META_DENY_DELETE)) {
        denyDelete = Boolean.parseBoolean(MetaUtil.getMetaAttribute(this.persistentClass, META_DENY_DELETE));
    } else {
        denyDelete = false;
    }

    // id
    PropertyMetadata propertyMetadata = new PropertyMetadataImpl(this,
            classMetadata.getIdentifierPropertyName());
    propertySet.add(propertyMetadata);
    propertyMap.put(propertyMetadata.getName(), propertyMetadata);

    // properties
    int i = 0;
    for (final String propertyName : classMetadata.getPropertyNames()) {
        if (!isBackref(propertyName) && !isVersionProperty(i) && !isDiscriminatorDuplicate(propertyName)) {
            propertyMetadata = new PropertyMetadataImpl(this, propertyName);
            propertySet.add(propertyMetadata);
            propertyMap.put(propertyMetadata.getName(), propertyMetadata);
        } else {
            logger.debug("ignoring property of " + classMetadata.getEntityName() + ": " + propertyName);
        }
        i++;
    }

    // subclasses
    if (persistentClass.hasSubclasses()) {
        subclasses = new ArrayList<Class<? extends Entity>>();
        for (final Iterator<?> iter = persistentClass.getSubclassIterator(); iter.hasNext();) {
            Subclass subclass = (Subclass) iter.next();
            try {
                subclasses.add((Class<? extends Entity>) Class.forName(subclass.getEntityName()));
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    } else {
        subclasses = Collections.emptyList();
    }
}