Example usage for java.sql Wrapper getClass

List of usage examples for java.sql Wrapper getClass

Introduction

In this page you can find the example usage for java.sql Wrapper getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.amalto.core.storage.hibernate.UpdateReportTypeMapping.java

private boolean isUsingClob(Wrapper data) {
    try {/*  ww w.  j a v a2 s.co m*/
        return data.getClass().getField("x_items_xml").getType().equals(Clob.class); //$NON-NLS-1$ 
    } catch (Exception e) {
        throw new RuntimeException("Could not check Items XML type", e); //$NON-NLS-1$
    }
}

From source file:com.amalto.core.storage.hibernate.EntityFinder.java

/**
 * Starting from <code>wrapper</code>, goes up the containment tree using references introspection in metadata.
 * @param wrapper A {@link Wrapper} instance (so an object managed by {@link HibernateStorage}.
 * @param storage A {@link HibernateStorage} instance. It will be used to compute references from the internal
 *                data model.//from  w ww  .j av  a 2  s.  co  m
 * @param session A Hibernate {@link Session}.
 * @return The top level (aka the Wrapper instance that represent a MDM entity).
 */
public static Wrapper findEntity(Wrapper wrapper, HibernateStorage storage, Session session) {
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    if (!(contextClassLoader instanceof StorageClassLoader)) {
        throw new IllegalStateException("Expects method to be called in the context of a storage operation.");
    }
    StorageClassLoader classLoader = (StorageClassLoader) contextClassLoader;
    ComplexTypeMetadata wrapperType = classLoader.getTypeFromClass(wrapper.getClass());
    if (wrapperType == null) {
        throw new IllegalArgumentException(
                "Wrapper '" + wrapper.getClass().getName() + "' isn't known in current storage.");
    }
    if (wrapperType.isInstantiable()) {
        return wrapper;
    }
    InboundReferences incomingReferences = new InboundReferences(wrapperType);
    InternalRepository internalRepository = storage.getTypeEnhancer();
    Set<ReferenceFieldMetadata> references = internalRepository.getInternalRepository()
            .accept(incomingReferences);
    if (references.isEmpty()) {
        throw new IllegalStateException("Cannot find container type for '" + wrapperType.getName() + "'.");
    }
    String keyFieldName = wrapperType.getKeyFields().iterator().next().getName();
    Object id = wrapper.get(keyFieldName);
    for (ReferenceFieldMetadata reference : references) {
        ComplexTypeMetadata containingType = reference.getContainingType();
        Class<? extends Wrapper> clazz = classLoader.getClassFromType(containingType);
        Criteria criteria = session.createCriteria(clazz, "a0"); //$NON-NLS-1$
        criteria.createAlias("a0." + reference.getName(), "a1", CriteriaSpecification.INNER_JOIN); //$NON-NLS-1$
        criteria.add(Restrictions.eq("a1." + keyFieldName, id)); //$NON-NLS-1$
        List list = criteria.list();
        if (!list.isEmpty()) {
            Wrapper container = (Wrapper) list.get(0);
            if (list.size() > 1) {
                Object previousItem = list.get(0);
                for (int i = 1; i < list.size(); i++) {
                    Object currentItem = list.get(i);
                    if (!previousItem.equals(currentItem)) {
                        throw new IllegalStateException("Expected contained instance to have only one owner.");
                    }
                    previousItem = currentItem;
                }
            }
            return findEntity(container, storage, session);
        }
    }
    return null;
}