Java Code Examples for org.hibernate.mapping.PersistentClass#getPropertyIterator()

The following examples show how to use org.hibernate.mapping.PersistentClass#getPropertyIterator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MetadataTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Test
public void testEntityToDatabaseBindingMetadata() {
    Metadata metadata = MetadataExtractorIntegrator.INSTANCE.getMetadata();

    for ( PersistentClass persistentClass : metadata.getEntityBindings()) {
        Table table = persistentClass.getTable();
        LOGGER.info( "Entity: {} is mapped to table: {}",
                     persistentClass.getClassName(),
                     table.getName()
        );

        for(Iterator propertyIterator =
                persistentClass.getPropertyIterator(); propertyIterator.hasNext(); ) {
            Property property = (Property) propertyIterator.next();
            for(Iterator columnIterator =
                    property.getColumnIterator(); columnIterator.hasNext(); ) {
                Column column = (Column) columnIterator.next();
                LOGGER.info( "Property: {} is mapped on table column: {} of type: {}",
                             property.getName(),
                             column.getName(),
                             column.getSqlType()
                );
            }
        }
    }
}
 
Example 2
Source File: HibernateUtil.java    From unitime with Apache License 2.0 5 votes vote down vote up
public static void fixSchemaInFormulas(Configuration cfg) throws ClassNotFoundException {
	cfg.buildMappings();
	Class dialect = Class.forName(cfg.getProperty("dialect"));
	String schema = cfg.getProperty("default_schema");
	for (Iterator i=cfg.getClassMappings();i.hasNext();) {
        PersistentClass pc = (PersistentClass)i.next();
        for (Iterator j=pc.getPropertyIterator();j.hasNext();) {
            Property p = (Property)j.next();
            for (Iterator k=p.getColumnIterator();k.hasNext();) {
                Selectable c = (Selectable)k.next();
                if (c instanceof Formula) {
                    Formula f = (Formula)c;
                    boolean updated = false;
                    if (schema != null && f.getFormula() != null && f.getFormula().indexOf("%SCHEMA%")>=0) {
                        f.setFormula(f.getFormula().replaceAll("%SCHEMA%", schema));
                        sLog.debug("Schema updated in "+pc.getClassName()+"."+p.getName()+" to "+f.getFormula());
                    }
                    if (f.getFormula()!=null && (f.getFormula().indexOf("%TRUE%")>=0 || f.getFormula().indexOf("%FALSE%")>=0)) {
                    	if (isPostgress(dialect)) {
                    		f.setFormula(f.getFormula().replaceAll("%TRUE%", "'t'").replaceAll("%FALSE%", "'f'"));
                    	} else {
                    		f.setFormula(f.getFormula().replaceAll("%TRUE%", "1").replaceAll("%FALSE%", "0"));
                    	}
                    }
                    if (updated)
                    	sLog.debug("Schema updated in "+pc.getClassName()+"."+p.getName()+" to "+f.getFormula());
                }
            }
        }
    }
}
 
Example 3
Source File: PojoEntityTuplizer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected ProxyFactory buildProxyFactory(PersistentClass persistentClass, Getter idGetter, Setter idSetter) {
	// determine the id getter and setter methods from the proxy interface (if any)
	// determine all interfaces needed by the resulting proxy
	
	/*
	 * We need to preserve the order of the interfaces they were put into the set, since javassist will choose the
	 * first one's class-loader to construct the proxy class with. This is also the reason why HibernateProxy.class
	 * should be the last one in the order (on JBossAS7 its class-loader will be org.hibernate module's class-
	 * loader, which will not see the classes inside deployed apps.  See HHH-3078
	 */
	Set<Class> proxyInterfaces = new java.util.LinkedHashSet<Class>();

	Class mappedClass = persistentClass.getMappedClass();
	Class proxyInterface = persistentClass.getProxyInterface();

	if ( proxyInterface != null && !mappedClass.equals( proxyInterface ) ) {
		if ( !proxyInterface.isInterface() ) {
			throw new MappingException(
					"proxy must be either an interface, or the class itself: " + getEntityName()
			);
		}
		proxyInterfaces.add( proxyInterface );
	}

	if ( mappedClass.isInterface() ) {
		proxyInterfaces.add( mappedClass );
	}

	Iterator<Subclass> subclasses = persistentClass.getSubclassIterator();
	while ( subclasses.hasNext() ) {
		final Subclass subclass = subclasses.next();
		final Class subclassProxy = subclass.getProxyInterface();
		final Class subclassClass = subclass.getMappedClass();
		if ( subclassProxy != null && !subclassClass.equals( subclassProxy ) ) {
			if ( !subclassProxy.isInterface() ) {
				throw new MappingException(
						"proxy must be either an interface, or the class itself: " + subclass.getEntityName()
				);
			}
			proxyInterfaces.add( subclassProxy );
		}
	}

	proxyInterfaces.add( HibernateProxy.class );

	Iterator properties = persistentClass.getPropertyIterator();
	Class clazz = persistentClass.getMappedClass();
	while ( properties.hasNext() ) {
		Property property = (Property) properties.next();
		Method method = property.getGetter( clazz ).getMethod();
		if ( method != null && Modifier.isFinal( method.getModifiers() ) ) {
			LOG.gettersOfLazyClassesCannotBeFinal( persistentClass.getEntityName(), property.getName() );
		}
		method = property.getSetter( clazz ).getMethod();
		if ( method != null && Modifier.isFinal( method.getModifiers() ) ) {
			LOG.settersOfLazyClassesCannotBeFinal( persistentClass.getEntityName(), property.getName() );
		}
	}

	Method idGetterMethod = idGetter == null ? null : idGetter.getMethod();
	Method idSetterMethod = idSetter == null ? null : idSetter.getMethod();

	Method proxyGetIdentifierMethod = idGetterMethod == null || proxyInterface == null ?
			null :
			ReflectHelper.getMethod( proxyInterface, idGetterMethod );
	Method proxySetIdentifierMethod = idSetterMethod == null || proxyInterface == null ?
			null :
			ReflectHelper.getMethod( proxyInterface, idSetterMethod );

	ProxyFactory pf = buildProxyFactoryInternal( persistentClass, idGetter, idSetter );
	try {
		pf.postInstantiate(
				getEntityName(),
				mappedClass,
				proxyInterfaces,
				proxyGetIdentifierMethod,
				proxySetIdentifierMethod,
				persistentClass.hasEmbeddedIdentifier() ?
						(CompositeType) persistentClass.getIdentifier().getType() :
						null
		);
	}
	catch (HibernateException he) {
		LOG.unableToCreateProxyFactory( getEntityName(), he );
		pf = null;
	}
	return pf;
}
 
Example 4
Source File: PojoEntityTuplizer.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected ProxyFactory buildProxyFactory(PersistentClass persistentClass, Getter idGetter, Setter idSetter) {
	// determine the id getter and setter methods from the proxy interface (if any)
       // determine all interfaces needed by the resulting proxy
	HashSet proxyInterfaces = new HashSet();
	proxyInterfaces.add( HibernateProxy.class );
	
	Class mappedClass = persistentClass.getMappedClass();
	Class proxyInterface = persistentClass.getProxyInterface();

	if ( proxyInterface!=null && !mappedClass.equals( proxyInterface ) ) {
		if ( !proxyInterface.isInterface() ) {
			throw new MappingException(
			        "proxy must be either an interface, or the class itself: " + 
			        getEntityName()
				);
		}
		proxyInterfaces.add( proxyInterface );
	}

	if ( mappedClass.isInterface() ) {
		proxyInterfaces.add( mappedClass );
	}

	Iterator iter = persistentClass.getSubclassIterator();
	while ( iter.hasNext() ) {
		Subclass subclass = ( Subclass ) iter.next();
		Class subclassProxy = subclass.getProxyInterface();
		Class subclassClass = subclass.getMappedClass();
		if ( subclassProxy!=null && !subclassClass.equals( subclassProxy ) ) {
			if ( !proxyInterface.isInterface() ) {
				throw new MappingException(
				        "proxy must be either an interface, or the class itself: " + 
				        subclass.getEntityName()
				);
			}
			proxyInterfaces.add( subclassProxy );
		}
	}

	Iterator properties = persistentClass.getPropertyIterator();
	Class clazz = persistentClass.getMappedClass();
	while ( properties.hasNext() ) {
		Property property = (Property) properties.next();
		Method method = property.getGetter(clazz).getMethod();
		if ( method != null && Modifier.isFinal( method.getModifiers() ) ) {
			log.error(
					"Getters of lazy classes cannot be final: " + persistentClass.getEntityName() + 
					"." + property.getName() 
				);
		}
		method = property.getSetter(clazz).getMethod();
           if ( method != null && Modifier.isFinal( method.getModifiers() ) ) {
			log.error(
					"Setters of lazy classes cannot be final: " + persistentClass.getEntityName() + 
					"." + property.getName() 
				);
		}
	}

	Method idGetterMethod = idGetter==null ? null : idGetter.getMethod();
	Method idSetterMethod = idSetter==null ? null : idSetter.getMethod();

	Method proxyGetIdentifierMethod = idGetterMethod==null || proxyInterface==null ? 
			null :
	        ReflectHelper.getMethod(proxyInterface, idGetterMethod);
	Method proxySetIdentifierMethod = idSetterMethod==null || proxyInterface==null  ? 
			null :
	        ReflectHelper.getMethod(proxyInterface, idSetterMethod);

	ProxyFactory pf = buildProxyFactoryInternal( persistentClass, idGetter, idSetter );
	try {
		pf.postInstantiate(
				getEntityName(),
				mappedClass,
				proxyInterfaces,
				proxyGetIdentifierMethod,
				proxySetIdentifierMethod,
				persistentClass.hasEmbeddedIdentifier() ?
		                (AbstractComponentType) persistentClass.getIdentifier().getType() :
		                null
		);
	}
	catch ( HibernateException he ) {
		log.warn( "could not create proxy factory for:" + getEntityName(), he );
		pf = null;
	}
	return pf;
}