LazyInitializationException: could not initialize proxy-the owning Session was closed


Solution 1

You have a classic case of LIE – Lazy Initialization error. This topic has been discussed so many times that I suggest to do a search on the forum and point to the hibernate documentation. Basically, you are using lazy relationships that require an open session (so the lazy loading can happen). Inside Spring you can use OpenSessionInViewFilter/Interceptor or the HibernateInterceptor to keep a Hibernate session open during a request or method calls.

Solution 2

@Entity

@Table(name=”gis_point”)

@org.hibernate.annotations.Proxy(lazy=false)

Entity {

}

http://forum.springsource.org/showthread.php?t=27993

http://www.myeclipseide.com/PNphpBB2-viewtopic-t-12830.html

http://stackoverflow.com/questions/345705/hibernate-lazyinitializationexception-could-not-initialize-proxy

Advertisements

Get SessionFactory Instance without using hibernate.cfg.xml


Database Config

package co.id.hanoman.btnmw.spp.conf;

import java.util.List;
import java.util.Properties;
import java.util.Vector;

public class DatabaseConfig {
	private static String MYSQL_DBMS = "MYSQL";
	private static String SQLSERVER_DBMS = "SQLServer";

	private static String SQLSERVER_DRIVER = "net.sourceforge.jtds.jdbc.Driver";
	private static String MYSQL_DRIVER = "com.mysql.jdbc.Driver";

	private String host;
	private String port;
	private String dbname;
	private String dbms;
	private String user;
	private String pass;
	private List<Class<?>> mapList;
	private Properties properties;

	public DatabaseConfig(String host, String port, String dbname, String dbms,
			String user, String pass) {
		mapList = new Vector<Class<?>>();
		properties = new Properties();
		this.host = host;
		this.port = port;
		this.dbname = dbname;
		this.dbms = dbms;
		this.user = user;
		this.pass = pass;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getDbname() {
		return dbname;
	}

	public void setDbname(String dbname) {
		this.dbname = dbname;
	}

	public String getDbms() {
		return dbms;
	}

	public void setDbms(String dbms) {
		this.dbms = dbms;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	public List<Class<?>> getAnnotatedClass() {
		return mapList;
	}

	public void setMapList(List<Class<?>> mapList) {
		this.mapList = mapList;
	}

	public String getDriver() {

		if (dbms.equalsIgnoreCase(MYSQL_DBMS)) {
			return MYSQL_DRIVER;
		} else if (dbms.equalsIgnoreCase(SQLSERVER_DBMS)) {
			return SQLSERVER_DRIVER;
		}

		return null;
	}

	public String getUrl() {
		System.out.println(dbms + MYSQL_DBMS);
		if (dbms.equalsIgnoreCase(MYSQL_DBMS)) {
			return String.format("jdbc:mysql://%s:%s/%s", host, port, dbname);
		} else if (dbms.equalsIgnoreCase(SQLSERVER_DBMS)) {
			return String.format("jdbc:jtds:sqlserver://%s:%s/%s", host, port,
					dbname);
		}

		return null;
	}

	public String getDialect() {
		if (dbms.equalsIgnoreCase(MYSQL_DBMS)) {
			return "org.hibernate.dialect.SQLServerDialect";
		} else if (dbms.equalsIgnoreCase(SQLSERVER_DBMS)) {
			return "org.hibernate.dialect.MySQLDialect";
		}

		return null;
	}

	public void addAnnotatedClass(Class<?> entity) {
		mapList.add(entity);
	}

	public void removeAnnotatedClass(Class<?> entity) {
		mapList.remove(entity);
	}
	
	public Properties getProperties() {
		return properties;
	}
	
	public void addProperty(String k, String v) {
		properties.setProperty(k, v);
	}
	
	public void removeProperty(String k) {
		properties.remove(k);
	}
}

Syncrhonizer Session Factory

package co.id.hanoman.btnmw.spp.dao;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;

import co.id.hanoman.btnmw.spp.conf.DatabaseConfig;
import co.id.hanoman.btnmw.spp.entity.Pembayaran;
import co.id.hanoman.btnmw.spp.entity.Tagihan;


public class SynchronizerSessionFactory {
	private static Logger logger = Logger
			.getLogger(SynchronizerSessionFactory.class);

	public static SessionFactory getSessionFactory(DatabaseConfig conf) {
		logger.debug("getSessionFactory invoked");
		
		try {
			conf.addAnnotatedClass(Pembayaran.class);
			conf.addAnnotatedClass(Tagihan.class);
			conf.addProperty("hibernate.hbm2ddl.auto", "update");
			return HibernateSessionFactory.getSessionFactory(conf);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
			throw e;
		}
	}

}

HibernateSessionFactory

package co.id.hanoman.btnmw.spp.dao;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;

import co.id.hanoman.btnmw.spp.conf.DatabaseConfig;
import co.id.hanoman.btnmw.spp.entity.Pembayaran;
import co.id.hanoman.btnmw.spp.entity.Tagihan;


public class SynchronizerSessionFactory {
	private static Logger logger = Logger
			.getLogger(SynchronizerSessionFactory.class);

	public static SessionFactory getSessionFactory(DatabaseConfig conf) {
		logger.debug("getSessionFactory invoked");
		
		try {
			conf.addAnnotatedClass(Pembayaran.class);
			conf.addAnnotatedClass(Tagihan.class);
			conf.addProperty("hibernate.hbm2ddl.auto", "update");
			return HibernateSessionFactory.getSessionFactory(conf);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
			throw e;
		}
	}

}

@Test
	public void testGetSessionFactory() {
		DatabaseConfig conf = new DatabaseConfig("localhost", "3306", "btn_usu", "mysql", "root", "root");
		conf.addAnnotatedClass(Tagihan.class);
		conf.addAnnotatedClass(Pembayaran.class);
		conf.addProperty("hibernate.hbm2ddl.auto", "create");
		sf = SynchronizerSessionFactory.getSessionFactory(conf);
		assertNotNull(sf);
		
		ITagihanDao tagihanDao = new TagihanDao(sf);
		tagihanDao.save(tagihan);
		
		IPembayaranDao pembayaranDao = new PembayaranDao(sf);
		pembayaranDao.save(pembayaran);
	}