⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 baserootdaosessionmethods.svm

📁 一个购房管理系统,JSF+Hibernate+Mssql2
💻 SVM
字号:
	protected static Map<String, SessionFactory> sessionFactoryMap;
	protected SessionFactory sessionFactory;

	protected Session session;
	protected final static ThreadLocal<Session> currentSession = new ThreadLocal<Session>();
	
	/**
	 * Return a new Session object that must be closed when the work has been completed.
	 * @return the active Session
	 */
	public Session getSession() {
		return getSession(
			getConfigurationFileName());
	}

	/**
	 * Return a new Session object that must be closed when the work has been completed.
	 * @param configFile the config file must match the meta attribute "config-file" in the hibernate mapping file
	 * @return the active Session
	 */
	protected Session getSession(String configFile) {
		if (null != session && session.isOpen()) return session;
		else if (null != sessionFactory) {
			Session s = currentSession.get();
			if (null == s || !s.isOpen()) {
				s = sessionFactory.openSession();
				currentSession.set(s);
			}
			return s;
		}
		else {
			Session s = currentSession.get();
			if (null == s || !s.isOpen()) {
				s = getSessionFactory(configFile).openSession();
				currentSession.set(s);
			}
			return s;
		}
	}

	public void setSession (Session session) {
		this.session = session;
	}

	/**
	 * Configure the session factory by reading hibernate config file
	 */
	public static void initialize () {
		$!{class.AbsoluteRootDAOClassName}.initialize(
			(String) null);
	}
	
	/**
	 * Configure the session factory by reading hibernate config file
	 * @param configFileName the name of the configuration file
	 */
	public static void initialize (String configFileName) {
		$!{class.AbsoluteRootDAOClassName}.initialize(
			configFileName,
			$!{class.AbsoluteRootDAOClassName}.getNewConfiguration(
				null));
	}

	public static void initialize (String configFileName, Configuration configuration) {
		if (null != sessionFactoryMap && null != sessionFactoryMap.get(configFileName)) return;
		else {
			if (null == configFileName) {
				configuration.configure();
				$!{class.AbsoluteRootDAOClassName}.setSessionFactory(
					null,
					configuration.buildSessionFactory());
			}
			else {
				configuration.configure(
					configFileName);
				$!{class.AbsoluteRootDAOClassName}.setSessionFactory(
					configFileName,
					configuration.buildSessionFactory());
			}
		}
	}

	/**
	 * Set the session factory
	 */
	public void setSessionFactory (SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/**
	 * Set the session factory
	 */
	protected static void setSessionFactory (String configFileName, SessionFactory sf) {
		if (null == configFileName) configFileName = "";
		if (null == sessionFactoryMap) sessionFactoryMap = new HashMap<String, SessionFactory>();
		sessionFactoryMap.put(
			configFileName,
			sf);
	}

	/**
	 * Return the SessionFactory that is to be used by these DAOs.  Change this
	 * and implement your own strategy if you, for example, want to pull the SessionFactory
	 * from the JNDI tree.
	 */
	public SessionFactory getSessionFactory() {
		if (null != sessionFactory) return sessionFactory;
		else return getSessionFactory(
		getConfigurationFileName());
	}

	public SessionFactory getSessionFactory(String configFileName) {
		if (null == configFileName) configFileName = "";
		if (null == sessionFactoryMap)
			initialize(configFileName);
		SessionFactory sf = (SessionFactory) sessionFactoryMap.get(configFileName);
		if (null == sf)
			throw new RuntimeException("The session factory for '" + configFileName + "' has not been initialized (or an error occured during initialization)");
		else
			return sf;
	}

	/**
	 * Close all sessions for the current thread
	 */
	public static void closeCurrentSession () {
		Session s = currentSession.get();
		if (null != s) {
			if (s.isOpen()) s.close();
			currentSession.set(null);
		}
	}

	/**
	 * Close the session
	 */
	public void closeSession (Session session) {
		if (null != session) session.close();
	}

	/**
	 * Begin the transaction related to the session
	 */
	public Transaction beginTransaction(Session s) {
		return s.beginTransaction();
	}

	/**
	 * Commit the given transaction
	 */
	public void commitTransaction(Transaction t) {
		t.commit();
	}

	/**
	 * Return a new Configuration to use.  This is not a mistake and is meant
	 * to be overridden in the RootDAO if you want to do something different.
	 * The file name is passed in so you have that to access.  The config file
	 * is read in the initialize method.
	 */
	 public static Configuration getNewConfiguration (String configFileName) {
	 	return new Configuration();
	 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -