001: /*
002: * Copyright (C) 2007 Rob Manning
003: * manningr@users.sourceforge.net
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package net.sourceforge.squirrel_sql.plugins.hibernate.util;
020:
021: import java.io.File;
022: import java.sql.SQLException;
023:
024: import net.sourceforge.squirrel_sql.client.gui.db.ISQLAliasExt;
025: import net.sourceforge.squirrel_sql.client.session.ISession;
026: import net.sourceforge.squirrel_sql.fw.sql.ISQLDatabaseMetaData;
027: import net.sourceforge.squirrel_sql.fw.sql.ISQLDriver;
028: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
029: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
030:
031: import org.hibernate.SessionFactory;
032: import org.hibernate.cfg.Configuration;
033: import org.hibernate.cfg.Environment;
034: import org.hibernate.dialect.Dialect;
035: import org.hibernate.dialect.DialectFactory;
036:
037: /**
038: * Class that provides utility methods for obtaining SessionFactory objects.
039: *
040: * @author manningr
041: *
042: */
043: public class HibernateUtil {
044:
045: /** Logger for this class. */
046: private static final ILogger s_log = LoggerController
047: .createLogger(HibernateUtil.class);
048:
049: public static SessionFactory getSessionFactory(ISession session,
050: String[] classPathEntries) throws SQLException {
051: SessionFactory result = null;
052: Dialect dialect = getHibernateDialectForSession(session);
053: String dialectName = dialect.getClass().getName();
054: Configuration cfg = new Configuration();
055: ISQLAliasExt alias = session.getAlias();
056: ISQLDriver driver = session.getDriver();
057: if (s_log.isDebugEnabled()) {
058: s_log.debug("getSessionFactory: url=" + alias.getUrl());
059: }
060: cfg.setProperty(Environment.URL, alias.getUrl());
061: cfg.setProperty(Environment.USER, alias.getUserName());
062: cfg.setProperty(Environment.PASS, alias.getPassword());
063: cfg
064: .setProperty(Environment.DRIVER, driver
065: .getDriverClassName());
066: cfg.setProperty(Environment.DIALECT, dialectName);
067: cfg.setProperty(Environment.SHOW_SQL, "true");
068: cfg.setProperty(Environment.HBM2DDL_AUTO, "create");
069: for (int i = 0; i < classPathEntries.length; i++) {
070: String file = classPathEntries[i];
071: cfg.addDirectory(new File(file));
072: }
073:
074: result = cfg.buildSessionFactory();
075: return result;
076: }
077:
078: /**
079: * Returns the Hibernate dialect for the specified session.
080: *
081: * @param session the SQuirreL ISession implementation
082: *
083: * @return an instance of Dialect.
084: *
085: * @throws SQLException
086: */
087: public static Dialect getHibernateDialectForSession(ISession session)
088: throws SQLException {
089: Dialect result = null;
090: ISQLDatabaseMetaData md = session.getMetaData();
091: String databaseName = md.getDatabaseProductName();
092: int databaseMajorVersion = md.getDatabaseMajorVersion();
093:
094: result = DialectFactory.determineDialect(databaseName,
095: databaseMajorVersion);
096:
097: if (s_log.isInfoEnabled()) {
098: s_log
099: .info("Dialect returned from DialectFactory for databaseName="
100: + databaseName
101: + " and databaseMajorVersion="
102: + databaseMajorVersion
103: + " was "
104: + result.getClass().getName());
105: }
106:
107: return result;
108: }
109:
110: }
|