Search in sources :

Example 1 with Subclass

use of org.hibernate.mapping.Subclass in project hibernate-orm by hibernate.

the class JoinedSubclassEntityPersister method processPersistentClassHierarchy.

private Set<String> processPersistentClassHierarchy(PersistentClass persistentClass, boolean isBase, SessionFactoryImplementor factory, String[][] mapping) {
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // collect all the class names that indicate that the "main table" of the given PersistentClass should be
    // included when one of the collected class names is used in TREAT
    final Set<String> classNames = new HashSet<String>();
    final Iterator itr = persistentClass.getDirectSubclasses();
    while (itr.hasNext()) {
        final Subclass subclass = (Subclass) itr.next();
        final Set<String> subclassSubclassNames = processPersistentClassHierarchy(subclass, false, factory, mapping);
        classNames.addAll(subclassSubclassNames);
    }
    classNames.add(persistentClass.getEntityName());
    if (!isBase) {
        MappedSuperclass msc = persistentClass.getSuperMappedSuperclass();
        while (msc != null) {
            classNames.add(msc.getMappedClass().getName());
            msc = msc.getSuperMappedSuperclass();
        }
        associateSubclassNamesToSubclassTableIndexes(persistentClass, classNames, mapping, factory);
    }
    return classNames;
}
Also used : Subclass(org.hibernate.mapping.Subclass) MappedSuperclass(org.hibernate.mapping.MappedSuperclass) Iterator(java.util.Iterator) HashSet(java.util.HashSet)

Example 2 with Subclass

use of org.hibernate.mapping.Subclass in project hibernate-orm by hibernate.

the class PojoEntityTuplizer method buildProxyFactory.

@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;
}
Also used : Subclass(org.hibernate.mapping.Subclass) ProxyFactory(org.hibernate.proxy.ProxyFactory) HibernateException(org.hibernate.HibernateException) Method(java.lang.reflect.Method) MappingException(org.hibernate.MappingException) Iterator(java.util.Iterator) PersistentClass(org.hibernate.mapping.PersistentClass) Property(org.hibernate.mapping.Property) CompositeType(org.hibernate.type.CompositeType)

Example 3 with Subclass

use of org.hibernate.mapping.Subclass in project hibernate-orm by hibernate.

the class IdTableHelper method needsIdTable.

public boolean needsIdTable(PersistentClass entityBinding) {
    // need id table if the entity has secondary tables (joins)
    if (entityBinding.getJoinClosureSpan() > 0) {
        return true;
    }
    // need an id table if the entity is part of either a JOINED or UNION inheritance
    // hierarchy.  We do not allow inheritance strategy mixing, so work on that assumption
    // here...
    final RootClass rootEntityBinding = entityBinding.getRootClass();
    final Iterator itr = rootEntityBinding.getSubclassIterator();
    if (itr.hasNext()) {
        final Subclass subclassEntityBinding = (Subclass) itr.next();
        if (subclassEntityBinding instanceof JoinedSubclass || subclassEntityBinding instanceof UnionSubclass) {
            return true;
        }
    }
    return false;
}
Also used : RootClass(org.hibernate.mapping.RootClass) JoinedSubclass(org.hibernate.mapping.JoinedSubclass) UnionSubclass(org.hibernate.mapping.UnionSubclass) Subclass(org.hibernate.mapping.Subclass) Iterator(java.util.Iterator) UnionSubclass(org.hibernate.mapping.UnionSubclass) JoinedSubclass(org.hibernate.mapping.JoinedSubclass)

Example 4 with Subclass

use of org.hibernate.mapping.Subclass in project hibernate-orm by hibernate.

the class PersistentClassVisitorTest method testProperCallbacks.

@Test
public void testProperCallbacks() {
    PersistentClassVisitorValidator vv = new PersistentClassVisitorValidator();
    new RootClass(metadataBuildingContext).accept(vv);
    new Subclass(new RootClass(metadataBuildingContext), metadataBuildingContext).accept(vv);
    new JoinedSubclass(new RootClass(metadataBuildingContext), metadataBuildingContext).accept(vv);
    new SingleTableSubclass(new RootClass(metadataBuildingContext), metadataBuildingContext).accept(vv);
    new UnionSubclass(new RootClass(metadataBuildingContext), metadataBuildingContext).accept(vv);
}
Also used : RootClass(org.hibernate.mapping.RootClass) JoinedSubclass(org.hibernate.mapping.JoinedSubclass) SingleTableSubclass(org.hibernate.mapping.SingleTableSubclass) UnionSubclass(org.hibernate.mapping.UnionSubclass) Subclass(org.hibernate.mapping.Subclass) UnionSubclass(org.hibernate.mapping.UnionSubclass) JoinedSubclass(org.hibernate.mapping.JoinedSubclass) SingleTableSubclass(org.hibernate.mapping.SingleTableSubclass) Test(org.junit.Test)

Aggregations

Subclass (org.hibernate.mapping.Subclass)4 Iterator (java.util.Iterator)3 JoinedSubclass (org.hibernate.mapping.JoinedSubclass)2 RootClass (org.hibernate.mapping.RootClass)2 UnionSubclass (org.hibernate.mapping.UnionSubclass)2 Method (java.lang.reflect.Method)1 HashSet (java.util.HashSet)1 HibernateException (org.hibernate.HibernateException)1 MappingException (org.hibernate.MappingException)1 MappedSuperclass (org.hibernate.mapping.MappedSuperclass)1 PersistentClass (org.hibernate.mapping.PersistentClass)1 Property (org.hibernate.mapping.Property)1 SingleTableSubclass (org.hibernate.mapping.SingleTableSubclass)1 ProxyFactory (org.hibernate.proxy.ProxyFactory)1 CompositeType (org.hibernate.type.CompositeType)1 Test (org.junit.Test)1