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

The following examples show how to use org.hibernate.mapping.PersistentClass#getMappedClass() . 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: MetamodelImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static EntityTypeImpl<?> buildEntityType(PersistentClass persistentClass, MetadataContext context) {
	final Class javaType = persistentClass.getMappedClass();
	context.pushEntityWorkedOn( persistentClass );
	final MappedSuperclass superMappedSuperclass = persistentClass.getSuperMappedSuperclass();
	AbstractIdentifiableType<?> superType = superMappedSuperclass == null
			? null
			: locateOrBuildMappedsuperclassType( superMappedSuperclass, context );
	//no mappedSuperclass, check for a super entity
	if ( superType == null ) {
		final PersistentClass superPersistentClass = persistentClass.getSuperclass();
		superType = superPersistentClass == null
				? null
				: locateOrBuildEntityType( superPersistentClass, context );
	}
	EntityTypeImpl entityType = new EntityTypeImpl(
			javaType,
			superType,
			persistentClass
	);

	context.registerEntityType( persistentClass, entityType );
	context.popEntityWorkedOn( persistentClass );
	return entityType;
}
 
Example 2
Source File: PojoInstantiator.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public PojoInstantiator(PersistentClass persistentClass, ReflectionOptimizer.InstantiationOptimizer optimizer) {
	this.mappedClass = persistentClass.getMappedClass();
	this.proxyInterface = persistentClass.getProxyInterface();
	this.embeddedIdentifier = persistentClass.hasEmbeddedIdentifier();
	this.optimizer = optimizer;

	try {
		constructor = ReflectHelper.getDefaultConstructor( mappedClass );
	}
	catch ( PropertyNotFoundException pnfe ) {
		log.info(
		        "no default (no-argument) constructor for class: " +
				mappedClass.getName() +
				" (class must be instantiated by Interceptor)"
		);
		constructor = null;
	}
}
 
Example 3
Source File: SchemaUtils.java    From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the {@link Interleaved} annotation on a table if it exists.
 */
public static Interleaved getInterleaveAnnotation(Table table, Metadata metadata) {
  for (PersistentClass pc : metadata.getEntityBindings()) {
    if (pc.getTable().equals(table) && pc.getMappedClass() != null) {
      Class<?> entityClass = pc.getMappedClass();
      return entityClass.getAnnotation(Interleaved.class);
    }
  }

  return null;
}
 
Example 4
Source File: ProxyDefinitions.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public static ProxyDefinitions createFromMetadata(Metadata storeableMetadata, PreGeneratedProxies preGeneratedProxies) {
    //Check upfront for any need across all metadata: would be nice to avoid initializing the Bytecode provider.
    LazyBytecode lazyBytecode = new LazyBytecode();
    if (needAnyProxyDefinitions(storeableMetadata)) {
        final HashMap<Class<?>, ProxyClassDetailsHolder> proxyDefinitionMap = new HashMap<>();
        try {
            for (PersistentClass persistentClass : storeableMetadata.getEntityBindings()) {
                if (needsProxyGeneration(persistentClass)) {
                    final Class mappedClass = persistentClass.getMappedClass();
                    final Class proxyClassDefinition = generateProxyClass(persistentClass, lazyBytecode,
                            preGeneratedProxies);
                    if (proxyClassDefinition == null) {
                        continue;
                    }
                    final boolean overridesEquals = ReflectHelper.overridesEquals(mappedClass);
                    try {
                        proxyDefinitionMap.put(mappedClass,
                                new ProxyClassDetailsHolder(overridesEquals, proxyClassDefinition.getConstructor()));
                    } catch (NoSuchMethodException e) {
                        throw new HibernateException(
                                "Failed to generate Enhanced Proxy: default constructor is missing for entity '"
                                        + mappedClass.getName() + "'. Please add a default constructor explicitly.");
                    }
                }
            }
        } finally {
            lazyBytecode.close();
        }
        return new ProxyDefinitions(proxyDefinitionMap);
    } else {
        return new ProxyDefinitions(Collections.emptyMap());
    }
}
 
Example 5
Source File: AbstractPropertyMapping.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private PersistentClass getCommonPersistentClass(PersistentClass clazz1, PersistentClass clazz2) {
	while ( clazz2 != null && clazz2.getMappedClass() != null && clazz1.getMappedClass() != null && !clazz2.getMappedClass()
			.isAssignableFrom( clazz1.getMappedClass() ) ) {
		clazz2 = clazz2.getSuperclass();
	}
	return clazz2;
}
 
Example 6
Source File: PojoEntityInstantiator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public PojoEntityInstantiator(
		EntityMetamodel entityMetamodel,
		PersistentClass persistentClass,
		ReflectionOptimizer.InstantiationOptimizer optimizer) {
	super(
			persistentClass.getMappedClass(),
			optimizer,
			persistentClass.hasEmbeddedIdentifier()
	);
	this.entityMetamodel = entityMetamodel;

	this.proxyInterface = persistentClass.getProxyInterface();
	this.applyBytecodeInterception = PersistentAttributeInterceptable.class.isAssignableFrom( persistentClass.getMappedClass() );
}
 
Example 7
Source File: BytecodeEnhancementMetadataPojoImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static BytecodeEnhancementMetadata from(PersistentClass persistentClass) {
	final Class mappedClass = persistentClass.getMappedClass();
	final boolean enhancedForLazyLoading = PersistentAttributeInterceptable.class.isAssignableFrom( mappedClass );
	final LazyAttributesMetadata lazyAttributesMetadata = enhancedForLazyLoading
			? LazyAttributesMetadata.from( persistentClass )
			: LazyAttributesMetadata.nonEnhanced( persistentClass.getEntityName() );

	return new BytecodeEnhancementMetadataPojoImpl(
			persistentClass.getEntityName(),
			mappedClass,
			enhancedForLazyLoading,
			lazyAttributesMetadata
	);
}
 
Example 8
Source File: PojoEntityTuplizer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public PojoEntityTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappedEntity) {
		super( entityMetamodel, mappedEntity );
		this.mappedClass = mappedEntity.getMappedClass();
		this.proxyInterface = mappedEntity.getProxyInterface();
		this.lifecycleImplementor = Lifecycle.class.isAssignableFrom( mappedClass );
		this.isBytecodeEnhanced = entityMetamodel.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading();

		String[] getterNames = new String[propertySpan];
		String[] setterNames = new String[propertySpan];
		Class[] propTypes = new Class[propertySpan];
		for ( int i = 0; i < propertySpan; i++ ) {
			getterNames[i] = getters[i].getMethodName();
			setterNames[i] = setters[i].getMethodName();
			propTypes[i] = getters[i].getReturnType();
		}

		if ( hasCustomAccessors || !Environment.useReflectionOptimizer() ) {
			optimizer = null;
		}
		else {
			// todo : YUCK!!!
			optimizer = Environment.getBytecodeProvider().getReflectionOptimizer(
					mappedClass,
					getterNames,
					setterNames,
					propTypes
			);
//			optimizer = getFactory().getSettings().getBytecodeProvider().getReflectionOptimizer(
//					mappedClass, getterNames, setterNames, propTypes
//			);
		}
	}
 
Example 9
Source File: PojoEntityTuplizer.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public PojoEntityTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappedEntity) {
		super( entityMetamodel, mappedEntity );
		this.mappedClass = mappedEntity.getMappedClass();
		this.proxyInterface = mappedEntity.getProxyInterface();
		this.lifecycleImplementor = Lifecycle.class.isAssignableFrom( mappedClass );
		this.validatableImplementor = Validatable.class.isAssignableFrom( mappedClass );

		Iterator iter = mappedEntity.getPropertyClosureIterator();
		while ( iter.hasNext() ) {
			Property property = (Property) iter.next();
			if ( property.isLazy() ) {
				lazyPropertyNames.add( property.getName() );
			}
		}

		String[] getterNames = new String[propertySpan];
		String[] setterNames = new String[propertySpan];
		Class[] propTypes = new Class[propertySpan];
		for ( int i = 0; i < propertySpan; i++ ) {
			getterNames[i] = getters[i].getMethodName();
			setterNames[i] = setters[i].getMethodName();
			propTypes[i] = getters[i].getReturnType();
		}

		if ( hasCustomAccessors || !Environment.useReflectionOptimizer() ) {
			optimizer = null;
		}
		else {
			// todo : YUCK!!!
			optimizer = Environment.getBytecodeProvider().getReflectionOptimizer( mappedClass, getterNames, setterNames, propTypes );
//			optimizer = getFactory().getSettings().getBytecodeProvider().getReflectionOptimizer(
//					mappedClass, getterNames, setterNames, propTypes
//			);
		}
	
	}
 
Example 10
Source File: ProxyDefinitions.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private static boolean needsProxyGeneration(PersistentClass persistentClass) {
    //Only lazy entities need a proxy, and only class-mapped classed can be proxies (Envers!)
    return persistentClass.isLazy() && (persistentClass.getMappedClass() != null);
}
 
Example 11
Source File: ProxyDefinitions.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private static Class<?> generateProxyClass(PersistentClass persistentClass,
        Supplier<ByteBuddyProxyHelper> byteBuddyProxyHelper,
        PreGeneratedProxies preGeneratedProxies) {
    final String entityName = persistentClass.getEntityName();
    final Class mappedClass = persistentClass.getMappedClass();
    if ((mappedClass.getModifiers() & ACC_FINAL) == ACC_FINAL) {
        LOGGER.warn("Could not generate an enhanced proxy for entity '" + entityName + "' (class='"
                + mappedClass.getCanonicalName()
                + "') as it's final. Your application might perform better if we're allowed to extend it.");
        return null;
    }
    final Set<Class> proxyInterfaces = ProxyFactoryHelper.extractProxyInterfaces(persistentClass, entityName);
    PreGeneratedProxies.ProxyClassDetailsHolder preProxy = preGeneratedProxies.getProxies()
            .get(persistentClass.getClassName());
    Class<?> preGeneratedProxy = null;
    boolean match = true;
    if (preProxy != null) {
        match = proxyInterfaces.size() == preProxy.getProxyInterfaces().size();
        if (match) {
            for (Class i : proxyInterfaces) {
                if (!preProxy.getProxyInterfaces().contains(i.getName())) {
                    match = false;
                    break;
                }
            }
        }
        if (match) {
            try {
                preGeneratedProxy = Class.forName(preProxy.getClassName(), false,
                        Thread.currentThread().getContextClassLoader());
            } catch (ClassNotFoundException e) {
                //should never happen
                throw new RuntimeException("Unable to load proxy class", e);
            }
        }
    }

    if (preGeneratedProxy == null) {
        if (match) {
            LOGGER.warnf("Unable to find a build time generated proxy for entity %s",
                    persistentClass.getClassName());
        } else {
            //TODO: this should be changed to an exception after 1.4
            //really it should be an exception now
            LOGGER.errorf(
                    "Unable to use a build time generated proxy for entity %s, as the build time proxy " +
                            "interfaces %s are different to the runtime ones %s. This should not happen, please open an " +
                            "issue at https://github.com/quarkusio/quarkus/issues",
                    persistentClass.getClassName(), preProxy.getProxyInterfaces(), proxyInterfaces);
        }
        Class<?> proxyDef = byteBuddyProxyHelper.get().buildProxy(mappedClass, toArray(proxyInterfaces));
        return proxyDef;
    } else {
        return preGeneratedProxy;
    }
}
 
Example 12
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 13
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;
}