/* * Copyright 2007-2008 the original author or authors. * * 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 com.cassevern.groovy.grails.orm.hibernate.metaclass; import org.codehaus.groovy.grails.orm.hibernate.metaclass.AbstractSavePersistentMethod; import org.codehaus.groovy.grails.commons.GrailsApplication; import org.hibernate.FlushMode; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.HibernateTemplate; import java.sql.SQLException; import java.util.regex.Pattern; /** * This method follows the semantics of save of scheduling the object * for persistence when a flush occurs. * The class was inspired by org.codehaus.groovy.grails.orm.hibernate.metaclass.SavePersistentMethod * * @author Don Denoncourt * * @since 1.0RC2 * * Created: Nov 10, 2007 */ public class InsertPersistentMethod extends AbstractSavePersistentMethod { public static final String METHOD_SIGNATURE = "save"; public static final Pattern METHOD_PATTERN = Pattern.compile('^'+METHOD_SIGNATURE+'$'); public InsertPersistentMethod(SessionFactory sessionFactory, ClassLoader classLoader, GrailsApplication application) { super(METHOD_PATTERN,sessionFactory, classLoader, application); } protected void performSave(final Object target, final boolean flush) { HibernateTemplate ht = getHibernateTemplate(); ht.execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { session.save(target); if(flush && FlushMode.isManualFlushMode(session.getFlushMode())) getHibernateTemplate().flush(); return target; } }); } }