Java Code Examples for org.hibernate.metadata.ClassMetadata.getPropertyTypes()

The following are Jave code examples for showing how to use getPropertyTypes() of the org.hibernate.metadata.ClassMetadata class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: open-cyclos   File: HibernateQueryHandler.java   View Source Code Vote up 6 votes
/**
 * Copies the persistent properties from the source to the destination entity
 */
public void copyProperties(final Entity source, final Entity dest) {
    if (source == null || dest == null) {
        return;
    }
    final ClassMetadata metaData = getClassMetaData(source);
    final Object[] values = metaData.getPropertyValues(source, EntityMode.POJO);
    // Skip the collections
    final Type[] types = metaData.getPropertyTypes();
    for (int i = 0; i < types.length; i++) {
        final Type type = types[i];
        if (type instanceof CollectionType) {
            values[i] = null;
        }
    }
    metaData.setPropertyValues(dest, values, EntityMode.POJO);
}
 
Example 2
Project: geomajas-project-server   File: HibernateEntityMapper.java   View Source Code Vote up 6 votes
/**
 * Construct a entity collection.
 *
 * @param parentMetadata parent meta data
 * @param childMetadata child meta data
 * @param parent parent object
 * @param objects child objects
 */
public HibernateEntityCollection(ClassMetadata parentMetadata, ClassMetadata childMetadata, Object parent,
		Collection<?> objects) {
	this.objects = objects;
	int i = 0;
	for (Type type : childMetadata.getPropertyTypes()) {
		if (type instanceof ManyToOneType) {
			ManyToOneType mto = (ManyToOneType) type;
			if (mto.getAssociatedEntityName().equals(parentMetadata.getEntityName())) {
				parentName = childMetadata.getPropertyNames()[i];
			}
		}
		i++;
	}
	this.metadata = childMetadata;
	this.parent = parent;
}
 
Example 3
Project: cacheonix-core   File: Printer.java   View Source Code Vote up 5 votes
/**
 * @param entity an actual entity object, not a proxy!
 */
public String toString(Object entity, EntityMode entityMode) throws HibernateException {

	// todo : this call will not work for anything other than pojos!
	ClassMetadata cm = factory.getClassMetadata( entity.getClass() );

	if ( cm==null ) return entity.getClass().getName();

	Map result = new HashMap();

	if ( cm.hasIdentifierProperty() ) {
		result.put(
			cm.getIdentifierPropertyName(),
			cm.getIdentifierType().toLoggableString( cm.getIdentifier( entity, entityMode ), factory )
		);
	}

	Type[] types = cm.getPropertyTypes();
	String[] names = cm.getPropertyNames();
	Object[] values = cm.getPropertyValues( entity, entityMode );
	for ( int i=0; i<types.length; i++ ) {
		if ( !names[i].startsWith("_") ) {
			String strValue = values[i]==LazyPropertyInitializer.UNFETCHED_PROPERTY ?
				values[i].toString() :
				types[i].toLoggableString( values[i], factory );
			result.put( names[i], strValue );
		}
	}
	return cm.getEntityName() + result.toString();
}
 
Example 4
Project: breeze.server.java   File: RelationshipFixer.java   View Source Code Vote up 5 votes
/**
 * Connect the related entities based on the foreign key values.
 * Note that this may cause related entities to be loaded from the DB if they are not already in the session.
 * @param entityInfo Entity that will be saved
 * @param meta Metadata about the entity type
 */
private void processRelationships(EntityInfo entityInfo, ClassMetadata meta) {
    addToGraph(entityInfo, null); // make sure every entity is in the graph
    String[] propNames = meta.getPropertyNames();
    Type[] propTypes = meta.getPropertyTypes();

    Type propType = meta.getIdentifierType();
    if (propType != null) {
        processRelationship(meta.getIdentifierPropertyName(), propType, entityInfo, meta);
    }

    for (int i = 0; i < propNames.length; i++) {
        processRelationship(propNames[i], propTypes[i], entityInfo, meta);
    }
}
 
Example 5
Project: open-cyclos   File: HibernateQueryHandler.java   View Source Code Vote up 5 votes
@SuppressWarnings("unchecked")
public void resolveReferences(final Entity entity) {
    final ClassMetadata meta = getClassMetaData(entity);
    final String[] names = meta.getPropertyNames();
    final Type[] types = meta.getPropertyTypes();
    for (int i = 0; i < types.length; i++) {
        final Type type = types[i];
        final String name = names[i];
        if (type instanceof EntityType) {
            // Properties that are relationships to other entities
            Entity rel = PropertyHelper.get(entity, name);
            if (rel instanceof EntityReference) {
                rel = getHibernateTemplate().load(EntityHelper.getRealClass(rel), rel.getId());
                PropertyHelper.set(entity, name, rel);
            }
        } else if (type instanceof CollectionType && !(type instanceof MapType)) {
            // Properties that are collections of other entities
            final Collection<?> current = PropertyHelper.get(entity, name);
            if (current != null && !(current instanceof PersistentCollection)) {
                // We must check that the collection is made of entities, since Hibernate supports collections os values
                boolean isEntityCollection = true;
                final Collection<Entity> resolved = ClassHelper.instantiate(current.getClass());
                for (final Object object : current) {
                    if (object != null && !(object instanceof Entity)) {
                        isEntityCollection = false;
                        break;
                    }
                    Entity e = (Entity) object;
                    if (object instanceof EntityReference) {
                        e = getHibernateTemplate().load(EntityHelper.getRealClass(e), e.getId());
                    }
                    resolved.add(e);
                }
                if (isEntityCollection) {
                    PropertyHelper.set(entity, name, resolved);
                }
            }
        }
    }
}