Example usage for org.hibernate SessionFactory getAllClassMetadata

List of usage examples for org.hibernate SessionFactory getAllClassMetadata

Introduction

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

Prototype

@Deprecated
Map<String, ClassMetadata> getAllClassMetadata();

Source Link

Document

Retrieve the ClassMetadata for all mapped entities.

Usage

From source file:org.jbpm.db.hibernate.HibernateHelper.java

License:Open Source License

public static void clearHibernateCache(SessionFactory sessionFactory) {
    sessionFactory.evictQueries();/*  w  w  w.j a  v a 2 s.c  o m*/

    Map classMetadata = sessionFactory.getAllClassMetadata();
    Iterator iter = classMetadata.keySet().iterator();
    while (iter.hasNext()) {
        String entityName = (String) iter.next();
        log.debug("evicting entities " + entityName);
        Class entityClass = ClassLoaderUtil.loadClass(entityName);
        sessionFactory.evict(entityClass);
    }

    Map collectionMetadata = sessionFactory.getAllCollectionMetadata();
    iter = collectionMetadata.keySet().iterator();
    while (iter.hasNext()) {
        String collectionName = (String) iter.next();
        log.debug("evicting collection " + collectionName);
        sessionFactory.evictCollection(collectionName);
    }
}

From source file:org.openspaces.persistency.hibernate.AbstractHibernateSpaceDataSource.java

License:Open Source License

private Set<String> createInitialLoadEntries(String[] initialLoadEntries, SessionFactory sessionFactory) {
    Set<String> result = new HashSet<String>();
    if (initialLoadEntries != null) {
        for (String entry : initialLoadEntries) {
            result.add(entry);// w  w  w  .j  a  va  2s  .  c  om
        }
    } else {
        // try and derive the managedEntries
        allMappedClassMetaData = sessionFactory.getAllClassMetadata();
        for (Map.Entry<String, ClassMetadata> entry : allMappedClassMetaData.entrySet()) {
            String entityName = entry.getKey();
            ClassMetadata classMetadata = entry.getValue();
            if (classMetadata.isInherited()) {
                String superClassEntityName = ((AbstractEntityPersister) classMetadata).getMappedSuperclass();
                ClassMetadata superClassMetadata = allMappedClassMetaData.get(superClassEntityName);
                Class superClass = superClassMetadata.getMappedClass();
                // only filter out classes that their super class has mappings
                if (superClass != null) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Entity [" + entityName + "] is inherited and has a super class ["
                                + superClass + "] filtering it out for initial load managedEntries");
                    }
                    continue;
                }
            }
            result.add(entityName);
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug(
                "Using Hibernate initial load managedEntries [" + Arrays.toString(initialLoadEntries) + "]");
    }
    return result;
}

From source file:org.openspaces.persistency.hibernate.ManagedEntitiesContainer.java

License:Open Source License

private static Set<String> createManagedEntries(Set<String> managedEntries, SessionFactory sessionFactory) {
    if (managedEntries == null) {
        managedEntries = new HashSet<String>();
        // try and derive the managedEntries
        Map<String, ClassMetadata> allClassMetaData = sessionFactory.getAllClassMetadata();
        for (String entityname : allClassMetaData.keySet()) {
            managedEntries.add(entityname);
        }/* ww w.  j av  a  2  s  .c om*/
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Using Hibernate managedEntries [" + managedEntries + "]");
    }
    return managedEntries;
}

From source file:org.opentaps.foundation.infrastructure.Infrastructure.java

License:Open Source License

/**
 * Gets the Hibernate <code>SessionFactory</code> object for the corresponding delegator.
 * A Map of delegatorName to SessionFactory is maintained, and the first time a SessionFactory is requested for.
 * A delegatorName, it is created in the the following way:
 * <ul>/*  ww  w  . j av  a 2 s  .  co  m*/
 * <li>First, get the data source for the group helper (ie, "org.ofbiz" in entitygroup.xml) and that delegator</li>
 * <li>Then, get the hibernate cfg xml for the data source</li>
 * <li>Finally, use that hibernate cfg xml to create a SessionFactory</li>
 * </ul>
 * @param delegatorName a <code>String</code> value
 * @return a Hibernate <code>SessionFactory</code> value
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static synchronized SessionFactory getSessionFactory(String delegatorName) {
    SessionFactory sessionFactory = sessionFactories.get(delegatorName);
    // the sessionFactory haven't init before
    if (sessionFactory == null) {
        Debug.logVerbose("building hibernate SessionFactory ...", MODULE);
        AnnotationConfiguration annotationConfiguration = new AnnotationConfiguration();
        Delegator delegator = DelegatorFactory.getDelegator(delegatorName);
        //for support eccas, construct persist event listener arrays
        PersistEventListener[] persistEventListeners = { new EcaPersistEventListener(delegator) };
        //for support eccas, construct load event listener arrays
        LoadEventListener[] loadEventListeners = { new EcaLoadEventListener(delegator) };
        //for support eccas, construct delete event listener arrays
        DeleteEventListener[] deleteEventListeners = { new EcaDeleteEventListener(delegator) };
        //for support eccas, construct saveOrUpdate event listener arrays
        SaveOrUpdateEventListener[] saveOrUpdateEventListeners = {
                new EcaSaveOrUpdateEventListener(delegator) };
        //for support eccas, construct saveOrUpdate event listener arrays
        SaveOrUpdateEventListener[] saveEventListeners = { new EcaSaveEventListener(delegator) };
        //register our event listener arrays
        annotationConfiguration.getEventListeners().setPersistEventListeners(persistEventListeners);
        annotationConfiguration.getEventListeners().setSaveOrUpdateEventListeners(saveOrUpdateEventListeners);
        annotationConfiguration.getEventListeners().setDeleteEventListeners(deleteEventListeners);
        annotationConfiguration.getEventListeners().setLoadEventListeners(loadEventListeners);
        annotationConfiguration.getEventListeners().setSaveEventListeners(saveEventListeners);
        annotationConfiguration.getEventListeners().setUpdateEventListeners(saveOrUpdateEventListeners);
        //get groupHelpName for retrieve database connection information.
        String groupHelperName = EntityConfigUtil.getDelegatorInfo(delegatorName).groupMap.get(getHelperName());
        Debug.logVerbose("groupHelperName : " + groupHelperName, MODULE);
        //get database source information
        DatasourceInfo datasourceInfo = EntityConfigUtil.getDatasourceInfo(groupHelperName);
        Debug.logVerbose("datasourceInfo.fieldTypeName : " + datasourceInfo.fieldTypeName, MODULE);
        //get hibernate dialect by database type
        String dialect = DIALECTS.get(datasourceInfo.fieldTypeName);
        if (dialect.length() == 0) {
            // some rarely used types are not supported
            Debug.logError("No hibernate dialect defined for the type [" + datasourceInfo.fieldTypeName + "]",
                    MODULE);
            return null;
        }
        annotationConfiguration.setProperty("hibernate.dialect", dialect);
        Debug.logVerbose("configuring SessionFactory ...", MODULE);
        //build a sessionFactory
        String datasourceName = EntityConfigUtil.getDelegatorInfo(delegatorName).groupMap.get(getHelperName());
        Debug.logVerbose("init sessionFactory by datasoure " + datasourceName, MODULE);
        sessionFactory = annotationConfiguration.configure(datasourceName + HIBERNATE_CFG_EXT)
                .buildSessionFactory();
        Debug.logVerbose("listing loaded entities ...", MODULE);
        //iterator all classes which are success load of hibernate, it just only for debug use.
        if (Debug.isOn(Debug.VERBOSE)) {
            Map metadata = sessionFactory.getAllClassMetadata();
            for (Iterator<EntityPersister> i = metadata.values().iterator(); i.hasNext();) {
                EntityPersister persister = i.next();
                String className = persister.getClassMetadata().getEntityName();
                Debug.logVerbose("SessionFactory Successfully Loaded AnnotatedClass : " + className, MODULE);
            }
        }
        sessionFactories.put(delegatorName, sessionFactory);
    }
    return sessionFactory;
}

From source file:org.opentaps.foundation.infrastructure.Infrastructure.java

License:Open Source License

/**
 * Clear hibernate second-level cache.//from   w w w . ja  v  a 2s.c  o  m
 * @param sessionFactory a <code>SessionFactory</code> instance
 */
@SuppressWarnings("unchecked")
public void evictHibernateCache(SessionFactory sessionFactory) {
    if (sessionFactory != null) {
        Map<String, CollectionMetadata> roleMap = sessionFactory.getAllCollectionMetadata();
        for (String roleName : roleMap.keySet()) {
            sessionFactory.evictCollection(roleName);
        }
        Map<String, ClassMetadata> entityMap = sessionFactory.getAllClassMetadata();
        for (String entityName : entityMap.keySet()) {
            sessionFactory.evictEntity(entityName);
        }
        sessionFactory.evictQueries();
    }
}

From source file:org.seasar.hibernate.jpa.PersistenceUnitConfigurationTest.java

License:Apache License

public void testReadPersistenceXmlFile() throws Exception {
    final Session session = Session.class.cast(em.getDelegate());
    final SessionFactory sf = session.getSessionFactory();
    assertEquals(2, sf.getAllClassMetadata().size());
    assertNotNull(sf.getClassMetadata(Employee.class));
    assertNotNull(sf.getClassMetadata(Department.class));
}

From source file:org.seasar.hibernate.jpa.PersistenceUnitConfigurationTest.java

License:Apache License

public void testAddMappingFile() throws Exception {
    final Session session = Session.class.cast(em.getDelegate());
    final SessionFactory sf = session.getSessionFactory();
    assertEquals(3, sf.getAllClassMetadata().size());
    assertNotNull(sf.getClassMetadata(Employee.class));
    assertNotNull(sf.getClassMetadata(Department.class));
    assertNotNull(sf.getClassMetadata(Employee2.class));
}

From source file:org.seasar.hibernate.jpa.PersistenceUnitConfigurationTest.java

License:Apache License

public void testAddPersistenceClass() throws Exception {
    final Session session = Session.class.cast(em.getDelegate());
    final SessionFactory sf = session.getSessionFactory();
    assertEquals(3, sf.getAllClassMetadata().size());
    assertNotNull(sf.getClassMetadata(Employee.class));
    assertNotNull(sf.getClassMetadata(Department.class));
    assertNotNull(sf.getClassMetadata(Department2.class));
}

From source file:org.seasar.hibernate.jpa.PersistenceUnitConfigurationTest.java

License:Apache License

public void testMappingFileAutoDetection() throws Exception {
    final Session session = Session.class.cast(em.getDelegate());
    final SessionFactory sf = session.getSessionFactory();
    assertEquals(5, sf.getAllClassMetadata().size());
    assertNotNull(sf.getClassMetadata(Employee.class));
    assertNotNull(sf.getClassMetadata(Department.class));
    assertNotNull(sf.getClassMetadata(Employee2.class));
    assertNotNull(sf.getClassMetadata(Address.class));
    assertNotNull(sf.getClassMetadata(Project.class));
}

From source file:org.seasar.hibernate.jpa.PersistenceUnitConfigurationTest.java

License:Apache License

public void testMappingFileAutoDetectionSubPackage() throws Exception {
    final PersistenceUnitManager pum = PersistenceUnitManager.class
            .cast(getComponent(PersistenceUnitManager.class));
    final EntityManagerFactory emf = pum.getEntityManagerFactory("aaaPersistenceUnit");
    final EntityManager em = emf.createEntityManager();
    final Session session = Session.class.cast(em.getDelegate());
    final SessionFactory sf = session.getSessionFactory();
    assertEquals(3, sf.getAllClassMetadata().size());
    assertNotNull(sf.getClassMetadata(Dummy2.class));
}