Java tutorial
/* Copyright (C) 2011 SpringSource * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.grails.orm.hibernate; import java.io.Serializable; import java.sql.SQLException; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Map; import javax.persistence.FlushModeType; import org.grails.datastore.mapping.model.PersistentProperty; import org.grails.datastore.mapping.model.config.GormProperties; import org.grails.datastore.mapping.proxy.ProxyHandler; import org.grails.datastore.mapping.query.event.PostQueryEvent; import org.grails.datastore.mapping.query.event.PreQueryEvent; import org.grails.orm.hibernate.proxy.HibernateProxyHandler; import org.grails.orm.hibernate.query.HibernateHqlQuery; import org.grails.orm.hibernate.query.HibernateQuery; import org.grails.datastore.mapping.model.PersistentEntity; import org.grails.datastore.mapping.query.Query; import org.grails.datastore.mapping.query.api.QueryableCriteria; import org.grails.datastore.mapping.query.jpa.JpaQueryBuilder; import org.grails.datastore.mapping.query.jpa.JpaQueryInfo; import org.grails.datastore.mapping.reflect.ClassPropertyFetcher; import org.hibernate.*; import org.hibernate.criterion.Restrictions; import org.hibernate.proxy.HibernateProxy; import org.springframework.context.ApplicationEventPublisher; /** * Session implementation that wraps a Hibernate {@link org.hibernate.Session}. * * @author Graeme Rocher * @since 1.0 */ @SuppressWarnings("rawtypes") public class HibernateSession extends AbstractHibernateSession { ProxyHandler proxyHandler = new HibernateProxyHandler(); public HibernateSession(HibernateDatastore hibernateDatastore, SessionFactory sessionFactory, int defaultFlushMode) { super(hibernateDatastore, sessionFactory); hibernateTemplate = new GrailsHibernateTemplate(sessionFactory, (HibernateDatastore) getDatastore()); } public HibernateSession(HibernateDatastore hibernateDatastore, SessionFactory sessionFactory) { this(hibernateDatastore, sessionFactory, hibernateDatastore.getDefaultFlushMode()); } @Override public Serializable getObjectIdentifier(Object instance) { if (instance == null) return null; if (proxyHandler.isProxy(instance)) { return ((HibernateProxy) instance).getHibernateLazyInitializer().getIdentifier(); } Class<?> type = instance.getClass(); ClassPropertyFetcher cpf = ClassPropertyFetcher.forClass(type); final PersistentEntity persistentEntity = getMappingContext().getPersistentEntity(type.getName()); if (persistentEntity != null) { return (Serializable) cpf.getPropertyValue(instance, persistentEntity.getIdentity().getName()); } return null; } /** * Deletes all objects matching the given criteria. * * @param criteria The criteria * @return The total number of records deleted */ public long deleteAll(final QueryableCriteria criteria) { return getHibernateTemplate().execute(new GrailsHibernateTemplate.HibernateCallback<Integer>() { public Integer doInHibernate(Session session) throws HibernateException, SQLException { JpaQueryBuilder builder = new JpaQueryBuilder(criteria); builder.setHibernateCompatible(true); JpaQueryInfo jpaQueryInfo = builder.buildDelete(); org.hibernate.Query query = session.createQuery(jpaQueryInfo.getQuery()); getHibernateTemplate().applySettings(query); List parameters = jpaQueryInfo.getParameters(); if (parameters != null) { for (int i = 0, count = parameters.size(); i < count; i++) { query.setParameter(JpaQueryBuilder.PARAMETER_NAME_PREFIX + (i + 1), parameters.get(i)); } } HibernateHqlQuery hqlQuery = new HibernateHqlQuery(HibernateSession.this, criteria.getPersistentEntity(), query); ApplicationEventPublisher applicationEventPublisher = datastore.getApplicationEventPublisher(); applicationEventPublisher.publishEvent(new PreQueryEvent(datastore, hqlQuery)); int result = query.executeUpdate(); applicationEventPublisher .publishEvent(new PostQueryEvent(datastore, hqlQuery, Collections.singletonList(result))); return result; } }); } /** * Updates all objects matching the given criteria and property values. * * @param criteria The criteria * @param properties The properties * @return The total number of records updated */ public long updateAll(final QueryableCriteria criteria, final Map<String, Object> properties) { return getHibernateTemplate().execute(new GrailsHibernateTemplate.HibernateCallback<Integer>() { public Integer doInHibernate(Session session) throws HibernateException, SQLException { JpaQueryBuilder builder = new JpaQueryBuilder(criteria); builder.setHibernateCompatible(true); PersistentEntity targetEntity = criteria.getPersistentEntity(); PersistentProperty lastUpdated = targetEntity.getPropertyByName(GormProperties.LAST_UPDATED); if (lastUpdated != null && targetEntity.getMapping().getMappedForm().isAutoTimestamp()) { properties.put(GormProperties.LAST_UPDATED, new Date()); } JpaQueryInfo jpaQueryInfo = builder.buildUpdate(properties); org.hibernate.Query query = session.createQuery(jpaQueryInfo.getQuery()); getHibernateTemplate().applySettings(query); List parameters = jpaQueryInfo.getParameters(); if (parameters != null) { for (int i = 0, count = parameters.size(); i < count; i++) { query.setParameter(JpaQueryBuilder.PARAMETER_NAME_PREFIX + (i + 1), parameters.get(i)); } } HibernateHqlQuery hqlQuery = new HibernateHqlQuery(HibernateSession.this, targetEntity, query); ApplicationEventPublisher applicationEventPublisher = datastore.getApplicationEventPublisher(); applicationEventPublisher.publishEvent(new PreQueryEvent(datastore, hqlQuery)); int result = query.executeUpdate(); applicationEventPublisher .publishEvent(new PostQueryEvent(datastore, hqlQuery, Collections.singletonList(result))); return result; } }); } public List retrieveAll(final Class type, final Iterable keys) { final PersistentEntity persistentEntity = getMappingContext().getPersistentEntity(type.getName()); return getHibernateTemplate().execute(new GrailsHibernateTemplate.HibernateCallback<List>() { public List doInHibernate(org.hibernate.Session session) throws HibernateException, SQLException { Criteria criteria = session.createCriteria(type); getHibernateTemplate().applySettings(criteria); criteria.add( Restrictions.in(persistentEntity.getIdentity().getName(), getIterableAsCollection(keys))); return new HibernateQuery(criteria, persistentEntity).list(); } }); } public Query createQuery(Class type) { return createQuery(type, null); } @Override public Query createQuery(Class type, String alias) { final PersistentEntity persistentEntity = getMappingContext().getPersistentEntity(type.getName()); Session currentSession = getHibernateTemplate().getSessionFactory().getCurrentSession(); final Criteria criteria = alias != null ? currentSession.createCriteria(type, alias) : currentSession.createCriteria(type); getHibernateTemplate().applySettings(criteria); return new HibernateQuery(criteria, this, persistentEntity); } protected GrailsHibernateTemplate getHibernateTemplate() { return (GrailsHibernateTemplate) getNativeInterface(); } public void setFlushMode(FlushModeType flushMode) { if (flushMode == FlushModeType.AUTO) { hibernateTemplate.setFlushMode(GrailsHibernateTemplate.FLUSH_AUTO); } else if (flushMode == FlushModeType.COMMIT) { hibernateTemplate.setFlushMode(GrailsHibernateTemplate.FLUSH_COMMIT); } } public FlushModeType getFlushMode() { switch (hibernateTemplate.getFlushMode()) { case GrailsHibernateTemplate.FLUSH_AUTO: return FlushModeType.AUTO; case GrailsHibernateTemplate.FLUSH_COMMIT: return FlushModeType.COMMIT; case GrailsHibernateTemplate.FLUSH_ALWAYS: return FlushModeType.AUTO; default: return FlushModeType.AUTO; } } }