/*
  GRANITE DATA SERVICES
  Copyright (C) 2007-2008 ADEQUATE SYSTEMS SARL

  This file is part of Granite Data Services.

  Granite Data Services is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 3 of the License, or (at your
  option) any later version.

  Granite Data Services is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

package org.granite.grails.integration;

import java.io.Serializable;
import java.lang.reflect.Method;

import org.granite.tide.data.JTAPersistenceContextManager;
import org.granite.util.Reflections;
import org.hibernate.SessionFactory;

/**
 * Responsible for attaching a session with the persistence mangager
 * @author cingram
 *
 */
public class GrailsHibernateSessionManager extends JTAPersistenceContextManager {
	
	private SessionFactory sessionFactory;
	
	
	public GrailsHibernateSessionManager() {
	}

	public GrailsHibernateSessionManager(SessionFactory sf) {
        this.sessionFactory = sf;
	}
    
	
	@Override
    protected Object beginTransaction() {
        return sessionFactory.getCurrentSession().getTransaction(); // We should be in a Spring managed transaction (or another container)
    }
    
	@Override
    protected void commitTransaction(Object tx) throws Exception {
	    // Should not commit: managed by the container
    }
    
	@Override
    protected void rollbackTransaction(Object tx) {
        // Should not rollback: managed by the container
    }

	
	/**
	 * attaches the entity to the JPA context.
	 * @return the attached entity
	 */
	@Override
    public Object mergeEntity(Object entity) {
        return findEntity(entity);
    }
	
    /**
     * Finds the entity with the JPA context.
     * @return the entity with the JPA context.
     */
	@Override
	public Object findEntity(Object entity) {
		if (entity == null)
			return null;
		
		try {
			Method getter = (Method)Reflections.getGetterMethod(entity.getClass(), "id");
			Serializable id = (Serializable)getter.invoke(entity);
			entity = sessionFactory.getCurrentSession().load(entity.getClass(), id);
	   	    return entity;
		}
		catch (Exception e) {
			throw new RuntimeException("Could not refresh entity " + entity.getClass(), e);
		}
	}
}

@@ -1016,16 +1016,45 @@
 tions;%0D%0A
+import org.hibernate.Query;%0D%0A
 import o
@@ -2070,20 +2070,17 @@
 erride%0D%0A
-    
+%09
 public O
@@ -2089,13 +2089,12 @@
 ect 
-merge
+find
 Enti
@@ -2113,224 +2113,24 @@
 tity
-) %7B%0D%0A        return findEntity(entity);%0D%0A    %7D%0D%0A%09%0D%0A    /**%0D%0A     * Finds the entity with the JPA context.%0D%0A     * @return the entity with the JPA context.%0D%0A     */%0D%0A%09@Override%0D%0A%09public Object findEntity(Object entity
+, String%5B%5D fetch
 ) %7B%0D
@@ -2327,24 +2327,51 @@
 y);%0D%0A%09%09%09
-entity =
+%0D%0A%09%09%09if (fetch == null)%0D%0A%09%09%09%09return
  session
@@ -2433,16 +2433,351 @@
 ;%0D%0A%09
-   %09    
+%09%09%0D%0A%09%09%09for (String f : fetch) %7B%0D%0A%09%09%09%09System.out.println(%22Fetching %22 + f);%0D%0A%09%09%09%09%0D%0A%09%09        Query q = sessionFactory.getCurrentSession().createQuery(%22select e from %22 + entity.getClass().getName() + %22 e left join fetch e.%22 + f + %22 where e = :entity%22);%0D%0A%09%09        q.setParameter(%22entity%22, entity);%0D%0A%09%09        entity = q.uniqueResult();%0D%0A%09%09%09%7D%0D%0A%09%09%09
 retu

Use left join fetch instead of Hibernate.initialize