Example usage for org.hibernate SessionFactory getClassMetadata

List of usage examples for org.hibernate SessionFactory getClassMetadata

Introduction

In this page you can find the example usage for org.hibernate SessionFactory getClassMetadata.

Prototype

@Deprecated
ClassMetadata getClassMetadata(String entityName);

Source Link

Document

Retrieve the ClassMetadata associated with the given entity class.

Usage

From source file:com.evolveum.midpoint.repo.sql.util.RUtil.java

License:Apache License

private static void fixCompositeIdentifierInMetaModel(SessionFactory sessionFactory, Class clazz) {
    ClassMetadata classMetadata = sessionFactory.getClassMetadata(clazz);
    if (classMetadata instanceof AbstractEntityPersister) {
        AbstractEntityPersister persister = (AbstractEntityPersister) classMetadata;
        EntityMetamodel model = persister.getEntityMetamodel();
        IdentifierProperty identifier = model.getIdentifierProperty();

        try {//from  ww  w .ja  va 2s  .c o  m
            Field field = IdentifierProperty.class.getDeclaredField("hasIdentifierMapper");
            field.setAccessible(true);
            field.set(identifier, true);
            field.setAccessible(false);
        } catch (Exception ex) {
            throw new SystemException(
                    "Attempt to fix entity meta model with hack failed, reason: " + ex.getMessage(), ex);
        }
    }
}

From source file:com.idega.jbpm.context.exe.matcher.HibernateLongIdMatcher.java

License:Open Source License

public boolean matches(final Object value) {

    Boolean res = getBpmContext().execute(new JbpmCallback() {

        public Object doInJbpm(JbpmContext context) throws JbpmException {
            @SuppressWarnings("unchecked")
            Class valueClass = value.getClass();
            if (value instanceof HibernateProxy) {
                valueClass = valueClass.getSuperclass();
            }//  ww  w.  j av a 2  s  .  c om

            boolean matches = false;

            SessionFactory sessionFactory = context.getSession().getSessionFactory();

            if (sessionFactory != null) {
                ClassMetadata classMetadata = sessionFactory.getClassMetadata(valueClass);
                matches = ((classMetadata != null)
                        && (classMetadata.getIdentifierType().getClass() == LongType.class));
            }
            return matches;
        }
    });

    return res;
}

From source file:com.idega.jbpm.context.exe.matcher.HibernateStringIdMatcher.java

License:Open Source License

public boolean matches(final Object value) {

    Boolean res = getBpmContext().execute(new JbpmCallback() {

        public Object doInJbpm(JbpmContext context) throws JbpmException {
            @SuppressWarnings("unchecked")
            Class valueClass = value.getClass();
            if (value instanceof HibernateProxy) {
                valueClass = valueClass.getSuperclass();
            }//  ww  w  . j  a  va  2 s .com

            boolean matches = false;

            SessionFactory sessionFactory = context.getSession().getSessionFactory();

            if (sessionFactory != null) {
                ClassMetadata classMetadata = sessionFactory.getClassMetadata(valueClass);
                matches = ((classMetadata != null)
                        && (classMetadata.getIdentifierType().getClass() == StringType.class));
            }
            return matches;
        }
    });

    return res;
}

From source file:com.mg.jet.birt.report.data.oda.ejbql.HibernateUtil.java

License:Open Source License

public static String[] getHibernateProp(String className) {
    Session session = HibernateUtil.currentSession();
    SessionFactory sf = session.getSessionFactory();
    String[] hibClassProps = sf.getClassMetadata(className).getPropertyNames();

    return (hibClassProps);
}

From source file:com.mg.jet.birt.report.data.oda.ejbql.HibernateUtil.java

License:Open Source License

public static String getHibernatePropTypes(String className, String propName) {

    Session session = HibernateUtil.currentSession();
    SessionFactory sf = session.getSessionFactory();
    org.hibernate.type.Type hibClassProps = sf.getClassMetadata(className).getPropertyType(propName);
    return (hibClassProps.getName());

}

From source file:com.mg.jet.birt.report.data.oda.ejbql.HibernateUtil.java

License:Open Source License

public static Object getHibernatePropVal(Object instObj, String className, String propName) {

    Session session = HibernateUtil.currentSession();
    SessionFactory sf = session.getSessionFactory();
    Object hibObj = sf.getClassMetadata(className).getPropertyValue(instObj, propName, EntityMode.POJO);
    return (hibObj);

}

From source file:com.oracle.coherence.hibernate.cachestore.HibernateCacheLoader.java

License:CDDL license

/**
 * Initializer (must be called post-constructor)
 * <p/>/* ww w . j av  a2  s  .c  o m*/
 * We do this specifically so that derived classes can safely create
 * override methods that depend on a fully constructed object state.
 * Will only be called once per instance and prior to the main body
 * of any API methods. This should not be directly called by derived
 * classes. If this method is overridden, super must be called at the
 * end of the overriding method.
 */
protected void initialize() {
    String sEntityName = m_sEntityName;

    if (sEntityName == null) {
        throw new IllegalStateException("Entity name attribute was not set");
    }

    SessionFactory sessionFactory = getSessionFactory();
    if (sessionFactory == null) {
        // Can only occur with derived classes
        throw new IllegalStateException(
                "No session factory was specified, " + "and a hibernate configuration file was not provided.");
    }

    // Look up the Hibernate metadata for the entity
    ClassMetadata entityClassMetadata = sessionFactory.getClassMetadata(sEntityName);
    if (entityClassMetadata == null) {
        throw new RuntimeException(
                "Unable to find ClassMetadata" + " for Hibernate entity " + sEntityName + ".");
    }
    setEntityClassMetadata(entityClassMetadata);

    // Create the loadAll query (it requires an identifier property).
    // Use "fetch all properties" to force eager loading.
    String sIdName;
    if (entityClassMetadata.hasIdentifierProperty()) {
        sIdName = entityClassMetadata.getIdentifierPropertyName();
        azzert(sIdName != null);
    } else {
        throw new RuntimeException(
                "Hibernate entity " + sEntityName + " does not have an ID column associated with it.");
    }

    String sLoadAllQuery = "from " + sEntityName + " fetch all properties where " + sIdName + " in (:"
            + PARAM_IDS + ") ";
    setLoadAllQuery(sLoadAllQuery);
}

From source file:com.syncnapsis.utils.HibernateUtil.java

License:Open Source License

/**
 * Get the ID type for an entity class/*  w  ww. j a  v  a2  s. c om*/
 * 
 * @param sessionFactory - the SessionFactory
 * @param clazz - the entity class
 * @return the ID type
 */
@SuppressWarnings("unchecked")
public static Class<? extends Serializable> getIdType(SessionFactory sessionFactory, Class<?> clazz) {
    if (!idTypes.containsKey(clazz))
        idTypes.put(clazz, sessionFactory.getClassMetadata(clazz).getIdentifierType().getReturnedClass());
    return idTypes.get(clazz);
}

From source file:com.vaadin.data.hbnutil.HbnContainer.java

License:Open Source License

/**
 * Constructor creates a new instance of HbnContainer.
 *//*from w  w w. j ava2s.  co  m*/
public HbnContainer(Class<T> entityType, SessionFactory sessionFactory) {
    logger.executionTrace();

    this.entityType = entityType;
    this.sessionFactory = sessionFactory;
    this.classMetadata = sessionFactory.getClassMetadata(entityType);

    this.cache = CacheBuilder.newBuilder().expireAfterAccess(2, TimeUnit.MINUTES).maximumSize(10000)
            .recordStats().weakValues().build(new CacheLoader<Object, EntityItem<T>>() {
                @Override
                public EntityItem<T> load(Object entityId) throws Exception {
                    try {
                        return loadEntity((Serializable) entityId);
                    } catch (Exception e) {
                        logger.error(e);
                        throw e;
                    }
                }
            });
}

From source file:com.wavemaker.runtime.data.task.AbstractReadTask.java

License:Open Source License

protected Object runQuery(Query query, boolean singleResult, String dbName) {

    if (singleResult) {
        return query.uniqueResult();
    } else {/*from  ww w .j  a  v a2s  .  c  o  m*/

        List<?> rs = query.list();

        SessionFactory sessFact = getSessionFactory(dbName);
        Type[] returnTypes = query.getReturnTypes();
        Class<?> returnedClass = returnTypes[0].getReturnedClass();
        String[] propertyNames = sessFact.getClassMetadata(returnedClass).getPropertyNames();
        Type[] propertyTypes = sessFact.getClassMetadata(returnedClass).getPropertyTypes();

        ObjectAccess oa = ObjectAccess.getInstance();

        //
        // To eliminate possible duplicates and construct a new list
        // with orders preserved for the final results
        //
        Iterator<?> rsItr = rs.iterator();
        List<Object> finalResultSet = new ArrayList<Object>();
        while (rsItr.hasNext()) {
            Object obj = rsItr.next();
            if (finalResultSet.contains(obj)) {
                continue;
            }

            for (int i = 0; i < propertyTypes.length; i++) {
                if (propertyTypes[i].getName().contains("lob")
                        || propertyTypes[i].getName().toLowerCase().contains("binary")) {
                    oa.setProperty(obj, propertyNames[i], null);
                }
            }
            finalResultSet.add(obj);
        }

        return finalResultSet;
    }
}