All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.beangle.spring.hibernate.LocalSessionFactoryBean.scala Maven / Gradle / Ivy

There is a newer version: 4.0.10
Show newest version
/*
 * Beangle, Agile Development Scaffold and Toolkit
 *
 * Copyright (c) 2005-2015, Beangle Software.
 *
 * Beangle 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.
 *
 * Beangle 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 Beangle.  If not, see .
 */
package org.beangle.spring.hibernate

import java.{ util => ju }
import org.beangle.commons.bean.{ Factory, Initializing }
import org.beangle.commons.lang.annotation.description
import org.beangle.commons.lang.reflect.Reflections
import org.beangle.data.jpa.hibernate.{ DefaultSessionFactoryBuilder, HbmSessionFactoryBuilder }
import org.hibernate.SessionFactory
import org.hibernate.cfg.{ Configuration, NamingStrategy }
import org.hibernate.cfg.AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS
import org.springframework.core.io.Resource
import javax.sql.DataSource
import org.beangle.data.jpa.hibernate.ConfigurableSessionFactory

@description("构建Hibernate的会话工厂")
class LocalSessionFactoryBean(val dataSource: DataSource) extends Factory[SessionFactory] with ConfigurableSessionFactory with Initializing {

  var configurationClass: Class[_ <: Configuration] = _

  /** static and global hbm mapping without namingstrategy */
  var staticHbm: Resource = _

  var configLocations: Array[Resource] = Array.empty

  var persistLocations: Array[Resource] = Array.empty

  var namingStrategy: NamingStrategy = _

  var hibernateProperties: ju.Properties = new ju.Properties

  /**For display informations*/
  var configuration: Configuration = _

  var result: SessionFactory = _

  def init() {
    var staticHbmInit = (null != staticHbm)
    if (null != staticHbm) {
      try {
        staticHbm.getFile
      } catch {
        case e: Exception => staticHbmInit = false
      }
    }
    if (staticHbmInit) {
      configuration = new Configuration
      val builder = new HbmSessionFactoryBuilder(dataSource, configuration, hibernateProperties)
      result = builder.build()
    } else {
      configuration = if (null != configurationClass) Reflections.newInstance(this.configurationClass) else new Configuration
      //  provide the Beangle managed Session as context
      configuration.getProperties.put(CURRENT_SESSION_CONTEXT_CLASS, classOf[BeangleSessionContext].getName())
      val builder = new DefaultSessionFactoryBuilder(dataSource, configuration, hibernateProperties)
      builder.namingStrategy = namingStrategy
      builder.configLocations = configLocations.map(l => l.getURL())
      builder.persistLocations = persistLocations.map(l => l.getURL())
      result = builder.build()
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy