List of usage examples for org.hibernate.event.service.spi EventListenerRegistry setListeners
<T> void setListeners(EventType<T> type, T... listeners);
From source file:org.dspace.app.cris.util.HibernateEventRegistry.java
public void init() { EventListenerRegistry eventListenerRegistry = ((SessionFactoryImpl) sessionFactory).getServiceRegistry() .getService(EventListenerRegistry.class); // If you wish to have custom determination and handling of "duplicate" // listeners, you would have to add an // implementation of the // org.hibernate.event.service.spi.DuplicationStrategy contract like // this/*from w w w .java 2 s .co m*/ // eventListenerRegistry.addDuplicationStrategy( myDuplicationStrategy // ); // EventListenerRegistry defines 3 ways to register listeners: // 1) This form overrides any existing registrations with // eventListenerRegistry.setListeners( EventType.AUTO_FLUSH, // myCompleteSetOfListeners ); // 2) This form adds the specified listener(s) to the beginning of the // listener chain // eventListenerRegistry.prependListeners( EventType.AUTO_FLUSH, // myListenersToBeCalledFirst ); // 3) This form adds the specified listener(s) to the end of the // listener chain // eventListenerRegistry.appendListeners( EventType.AUTO_FLUSH, // myListenersToBeCalledLast ); for (String eTypeStr : eventListeners.keySet()) { EventType eType = EventType.resolveEventTypeByName(eTypeStr); for (Object listener : eventListeners.get(eTypeStr)) { eventListenerRegistry.setListeners(eType, listener); } } }
From source file:org.eclipse.emf.cdo.server.internal.hibernate.HibernateStore.java
License:Open Source License
public synchronized SessionFactory getHibernateSessionFactory() { if (cdoDataStore == null) { if (TRACER.isEnabled()) { TRACER.trace("Initializing SessionFactory for HibernateStore"); //$NON-NLS-1$ }//w w w. j a v a 2 s . c o m currentHibernateStore.set(this); identifierPropertyNameByEntity = new HashMap<String, String>(); try { initDataStore(); // this has to be done before the classmapping is iterated // otherwise it is not initialized SessionFactory hibernateSessionFactory = cdoDataStore.getSessionFactory(); ServiceRegistry serviceRegistry = ((SessionFactoryImpl) hibernateSessionFactory) .getServiceRegistry(); final EventListenerRegistry eventListenerRegistry = serviceRegistry .getService(EventListenerRegistry.class); eventListenerRegistry.setListeners(EventType.MERGE, new CDOMergeEventListener()); final Iterator<?> iterator = cdoDataStore.getConfiguration().getClassMappings(); while (iterator.hasNext()) { final PersistentClass pc = (PersistentClass) iterator.next(); if (pc.getIdentifierProperty() == null) { // happens for featuremaps for now... continue; } identifierPropertyNameByEntity.put(pc.getEntityName(), pc.getIdentifierProperty().getName()); } } catch (Throwable t) { t.printStackTrace(System.err); if (TRACER.isEnabled()) { TRACER.trace(t); } throw new RuntimeException(t); } finally { currentHibernateStore.set(null); } } return cdoDataStore.getSessionFactory(); }
From source file:org.grails.orm.hibernate.EventListenerIntegrator.java
License:Apache License
@SuppressWarnings("unchecked") protected <T> void appendListeners(final EventListenerRegistry listenerRegistry, final EventType<T> eventType, final Map<String, Object> listeners) { Object listener = listeners.get(eventType.eventName()); if (listener != null) { if (shouldOverrideListeners(eventType, listener)) { // since ClosureEventTriggeringInterceptor extends DefaultSaveOrUpdateEventListener we want to override instead of append the listener here // to avoid there being 2 implementations which would impact performance too listenerRegistry.setListeners(eventType, (T) listener); } else {/* ww w .ja va2 s.com*/ listenerRegistry.appendListeners(eventType, (T) listener); } } }
From source file:org.granite.test.tide.hibernate4.data.TestHibernate4JPAChangeSetMerge.java
License:Open Source License
@Override protected void initPersistence() { Map<String, String> props = new HashMap<String, String>(); props.put("hibernate.dialect", org.hibernate.dialect.H2Dialect.class.getName()); props.put("hibernate.hbm2ddl.auto", "create-drop"); props.put("hibernate.show_sql", "true"); props.put("hibernate.connection.driver_class", org.h2.Driver.class.getName()); props.put("hibernate.connection.url", "jdbc:h2:mem:test-changesetmerge"); props.put("hibernate.connection.username", "sa"); props.put("hibernate.connection.password", ""); entityManagerFactory = Persistence.createEntityManagerFactory("hibernate4-changesetmerge-pu", props); try {/*w w w . j av a 2 s . c o m*/ SessionFactoryImpl sessionFactory = (SessionFactoryImpl) entityManagerFactory.getClass() .getMethod("getSessionFactory").invoke(entityManagerFactory); EventListenerRegistry registry = sessionFactory.getServiceRegistry() .getService(EventListenerRegistry.class); registry.setListeners(EventType.MERGE, new HibernateDataChangeMergeListener()); } catch (Exception e) { throw new RuntimeException("Could not init persistence", e); } }