List of usage examples for org.hibernate Session getSessionFactory
SessionFactory getSessionFactory();
From source file:com.formkiq.core.dao.DaoTests.java
License:Apache License
/** * Truncate database./* w w w .ja va2s .c o m*/ * @param em EntityManager */ @SuppressWarnings("resource") protected void truncateDatabase(final EntityManager em) { List<String> tableNames = new ArrayList<>(); Session session = em.unwrap(Session.class); Map<String, ClassMetadata> hibernateMetadata = session.getSessionFactory().getAllClassMetadata(); List<String> userTables = getUserTables(); for (ClassMetadata metadata : hibernateMetadata.values()) { AbstractEntityPersister aep = (AbstractEntityPersister) metadata; if (!userTables.contains(aep.getTableName())) { tableNames.add(aep.getTableName()); } } truncateTables(em, tableNames); truncateTables(em, getUserTables()); }
From source file:com.googlecode.hibernate.audit.HibernateAudit.java
License:Open Source License
public static AuditConfiguration getAuditConfiguration(Session session) { return AuditSessionFactoryObserver.getAuditConfiguration(session.getSessionFactory()); }
From source file:com.googlecode.hibernate.audit.listener.AuditSessionFactoryObserver.java
License:Open Source License
private AuditTypeField initializeAuditField(Session session, String auditTypeClassName, AuditType auditType, String propertyName, Type type) { AuditTypeField auditField = HibernateAudit.getAuditField(session, auditTypeClassName, propertyName); if (auditField == null) { auditField = new AuditTypeField(); auditField.setName(propertyName); auditField.setOwnerType(auditType); auditType.getAuditFields().add(auditField); AuditType auditFieldType = null; if (type.isCollectionType()) { auditFieldType = initializePrimitiveAuditType(session, type); Type elementType = ((CollectionType) type) .getElementType((SessionFactoryImplementor) session.getSessionFactory()); if (elementType.isCollectionType()) { // do nothing.. } else if (elementType.isComponentType()) { initializeComponentAuditType(session, (ComponentType) elementType); } else if (elementType.isEntityType()) { // do nothing .. it will be handled during the entity model // traverse } else { initializePrimitiveAuditType(session, elementType); }/*from ww w .j ava2 s.c o m*/ } else if (type.isComponentType()) { auditFieldType = initializeComponentAuditType(session, (CompositeType) type); } else if (type.isEntityType()) { auditFieldType = initializeEntityAuditType(session, ((EntityType) type).getName(), false); } else { auditFieldType = initializePrimitiveAuditType(session, type); } auditField.setFieldType(auditFieldType); session.save(auditField); updateMetaModel(session); } return auditField; }
From source file:com.googlecode.hibernate.audit.listener.AuditSessionFactoryObserver.java
License:Open Source License
private void updateMetaModel(Session session) { session.flush();//from w w w .jav a 2 s. c om NamedQueryDefinition selectAuditTypeNamedQueryDefinition = ((SessionFactoryImplementor) session .getSessionFactory()).getNamedQuery(HibernateAudit.SELECT_AUDIT_TYPE_BY_CLASS_NAME); NamedQueryDefinition selectAuditTypeFieldNamedQueryDefinition = ((SessionFactoryImplementor) session .getSessionFactory()) .getNamedQuery(HibernateAudit.SELECT_AUDIT_TYPE_FIELD_BY_CLASS_NAME_AND_PROPERTY_NAME); if (selectAuditTypeNamedQueryDefinition.isCacheable() && selectAuditTypeNamedQueryDefinition.getCacheRegion() != null) { session.getSessionFactory().getCache() .evictQueryRegion(selectAuditTypeNamedQueryDefinition.getCacheRegion()); } if (selectAuditTypeFieldNamedQueryDefinition.isCacheable() && selectAuditTypeFieldNamedQueryDefinition.getCacheRegion() != null) { session.getSessionFactory().getCache() .evictQueryRegion(selectAuditTypeFieldNamedQueryDefinition.getCacheRegion()); } session.getSessionFactory().getCache().evictEntityRegion(AuditType.class.getName()); session.getSessionFactory().getCache().evictEntityRegion(AuditTypeField.class.getName()); }
From source file:com.googlecode.hibernate.audit.synchronization.work.AbstractAuditWorkUnit.java
License:Open Source License
protected EntityObjectProperty processEntityProperty(Session session, AuditConfiguration auditConfiguration, Object object, String propertyName, Object propertyValue, Type propertyType, AuditObject auditObject) { String entityName = ((EntityType) propertyType).getAssociatedEntityName(); Serializable id = null;// www. jav a2s. c om AuditTypeField auditField = HibernateAudit .getAuditField(session, auditConfiguration.getExtensionManager().getAuditableInformationProvider() .getAuditTypeClassName(auditConfiguration.getMetadata(), getEntityName()), propertyName); if (propertyValue != null) { id = session.getSessionFactory().getClassMetadata(entityName).getIdentifier(propertyValue, (SessionImplementor) session); } EntityObjectProperty property = new EntityObjectProperty(); property.setAuditObject(auditObject); property.setAuditField(auditField); property.setIndex(null); property.setTargetEntityId( auditConfiguration.getExtensionManager().getPropertyValueConverter().toString(propertyType, id)); return property; }
From source file:com.googlecode.hibernate.audit.synchronization.work.AbstractAuditWorkUnit.java
License:Open Source License
protected AuditLogicalGroup getAuditLogicalGroup(Session session, AuditConfiguration auditConfiguration, AuditEvent auditEvent) {// w ww . ja v a 2s . c om AuditLogicalGroup logicalGroup = auditConfiguration.getExtensionManager().getAuditLogicalGroupProvider() .getAuditLogicalGroup(session, auditEvent); AuditLogicalGroup result = null; if (logicalGroup != null) { AuditType auditType = HibernateAudit.getAuditType(session, logicalGroup.getAuditType().getClassName()); String externalId = logicalGroup.getExternalId(); result = HibernateAudit.getAuditLogicalGroup(session, auditType, externalId); HibernateException createAuditLogicalGroupException = null; if (result == null) { createAuditLogicalGroupException = createAuditLogicalGroup(session, logicalGroup, auditType); // remove the cached query (possibly null) results so that the result after that is not null. NamedQueryDefinition namedQueryDefinition = ((SessionFactoryImplementor) session .getSessionFactory()).getNamedQuery( HibernateAudit.SELECT_AUDIT_LOCAL_GROUP_BY_AUDIT_TYPE_AND_EXTERNAL_ID); if (namedQueryDefinition.isCacheable()) { String cacheRegion = ((SessionFactoryImplementor) session.getSessionFactory()) .getNamedQuery(HibernateAudit.SELECT_AUDIT_LOCAL_GROUP_BY_AUDIT_TYPE_AND_EXTERNAL_ID) .getCacheRegion(); if (cacheRegion != null) { session.getSessionFactory().getCache().evictQueryRegion(cacheRegion); } else { session.getSessionFactory().getCache().evictQueryRegions(); } } result = HibernateAudit.getAuditLogicalGroup(session, auditType, externalId); } if (result == null) { if (createAuditLogicalGroupException != null) { throw createAuditLogicalGroupException; } else { throw new HibernateException("Unable to create and then retrieve AuditLogicalGroup: className=" + logicalGroup.getAuditType().getClassName() + ",externalId=" + logicalGroup.getExternalId()); } } auditLogicalGroups.add(result); } return result; }
From source file:com.googlecode.hibernate.audit.synchronization.work.AbstractAuditWorkUnit.java
License:Open Source License
private HibernateException createAuditLogicalGroup(Session session, AuditLogicalGroup logicalGroup, AuditType auditType) {//ww w .j av a2 s . c o m Session newSession = null; TransactionManager txManager = null; javax.transaction.Transaction suspendedTransaction = null; try { JtaPlatform jtaPlatform = ((SessionFactoryImplementor) session.getSessionFactory()).getSettings() .getJtaPlatform(); if (jtaPlatform != null) { txManager = jtaPlatform.retrieveTransactionManager(); } if (txManager != null) { try { suspendedTransaction = txManager.suspend(); } catch (SystemException e) { throw new HibernateException(e); } } Transaction tx = null; try { newSession = session.getSessionFactory().openSession(); tx = newSession.beginTransaction(); logicalGroup.setAuditType(auditType); // when we are creating the audit logical group for the first time we set it to 0, this is before even we may have transaction record - this is executed in separate transaction logicalGroup.setLastUpdatedAuditTransactionId(Long.valueOf(0)); newSession.save(logicalGroup); tx.commit(); return null; } catch (HibernateException e) { if (log.isDebugEnabled()) { // log the exception is debug level because this most likely // will indicate that there was a concurrent insert and we // are prepared to handle such calls. If this is not the // case and we want to troubleshoot where is the problem // then at least log the exception is DEBUG level so we can // see it. log.debug( "HibernateException occured while creating a new AuditLogicalGroup in new transaction", e); } if (tx != null) { try { tx.rollback(); } catch (HibernateException ignored) { } } return e; } finally { if (newSession != null && newSession.isOpen()) { try { newSession.close(); } catch (HibernateException ignored) { } } } } finally { if (txManager != null && suspendedTransaction != null) { try { txManager.resume(suspendedTransaction); } catch (SystemException e) { throw new HibernateException(e); } catch (InvalidTransactionException e) { throw new HibernateException(e); } } } }
From source file:com.googlecode.hibernate.audit.synchronization.work.AbstractCollectionAuditWorkUnit.java
License:Open Source License
protected void processElement(Session session, AuditConfiguration auditConfiguration, Object entityOwner, Object element, Type elementType, String propertyName, long index, EntityAuditObject auditObject, AuditEvent auditEvent) {/*w w w . j a v a 2 s .com*/ AuditTypeField auditField = HibernateAudit .getAuditField(session, auditConfiguration.getExtensionManager().getAuditableInformationProvider() .getAuditTypeClassName(auditConfiguration.getMetadata(), getEntityName()), propertyName); AuditObjectProperty property = null; if (elementType.isEntityType()) { Serializable id = null; if (element != null) { id = session.getSessionFactory() .getClassMetadata(((EntityType) elementType).getAssociatedEntityName()) .getIdentifier(element, (SessionImplementor) session); } property = new EntityObjectProperty(); property.setAuditObject(auditObject); property.setAuditField(auditField); property.setIndex(new Long(index)); ((EntityObjectProperty) property).setTargetEntityId( auditConfiguration.getExtensionManager().getPropertyValueConverter().toString(null, id)); } else if (elementType.isComponentType()) { CompositeType componentType = (CompositeType) elementType; property = new ComponentObjectProperty(); property.setAuditObject(auditObject); property.setAuditField(auditField); property.setIndex(new Long(index)); ComponentAuditObject targetComponentAuditObject = null; if (element != null) { targetComponentAuditObject = new ComponentAuditObject(); targetComponentAuditObject.setAuditEvent(auditEvent); targetComponentAuditObject.setParentAuditObject(auditObject); AuditType auditComponentType = HibernateAudit.getAuditType(session, auditConfiguration.getExtensionManager().getAuditableInformationProvider() .getAuditTypeClassName(auditConfiguration.getMetadata(), elementType)); targetComponentAuditObject.setAuditType(auditComponentType); for (int j = 0; j < componentType.getPropertyNames().length; j++) { String componentPropertyName = componentType.getPropertyNames()[j]; Type componentPropertyType = componentType.getSubtypes()[j]; Object componentPropertyValue = componentType.getPropertyValue(element, j, (SessionImplementor) session); processProperty(session, auditConfiguration, auditEvent, element, componentPropertyName, componentPropertyValue, componentPropertyType, targetComponentAuditObject); } } ((ComponentObjectProperty) property).setTargetComponentAuditObject(targetComponentAuditObject); } else if (elementType.isCollectionType()) { // collection of collections } else { property = new SimpleObjectProperty(); property.setAuditObject(auditObject); property.setAuditField(auditField); property.setIndex(new Long(index)); ((SimpleObjectProperty) property).setValue( auditConfiguration.getExtensionManager().getPropertyValueConverter().toString(null, element)); } if (property != null) { AuditType auditType = null; if (element != null) { auditType = HibernateAudit.getAuditType(session, element.getClass().getName()); if (auditType == null) { // subclass that was not registered in the audit metadata - use the base class auditType = property.getAuditField().getFieldType(); } } property.setAuditType(auditType); auditObject.getAuditObjectProperties().add(property); } }
From source file:com.hazelcast.hibernate.instance.HazelcastAccessor.java
License:Open Source License
/** * Tries to extract <code>HazelcastInstance</code> from <code>Session</code>. * * @param session//from ww w . j a v a2 s .c o m * @return Currently used <code>HazelcastInstance</code> or null if an error occurs. */ public static HazelcastInstance getHazelcastInstance(final Session session) { return getHazelcastInstance(session.getSessionFactory()); }
From source file:com.hihsoft.sso.sysmonitor.sysaudit.listener.HistoryEventListener.java
License:Apache License
private void recordHistory(Session session, TlogAudit entry) { Session tempSession = session.getSessionFactory().openSession(); Transaction tx = tempSession.beginTransaction(); try {/*from ww w.ja v a 2 s . c o m*/ tx.begin(); tempSession.save(entry); tempSession.flush(); tx.commit(); } catch (Exception ex) { tx.rollback(); } tempSession.close(); }