Skip to content

Instantly share code, notes, and snippets.

@agnaldo4j
Created July 6, 2013 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agnaldo4j/5941364 to your computer and use it in GitHub Desktop.
Save agnaldo4j/5941364 to your computer and use it in GitHub Desktop.
Exemplo de criação de SessionFactory com Scala e Hibernate 4.2.
package
import org.hibernate.service.{ServiceRegistryBuilder, ServiceRegistry}
import org.hibernate.cfg.Configuration
import org.hibernate.{Session, SessionFactory}
import scala.collection.JavaConversions._
object HibernateSessionFactory {
def buildSessionFactory(dbDriver:String = "org.postgresql.Driver",
dbDialect:String = "org.hibernate.dialect.PostgreSQLDialect",
dbPath:String = "jdbc:postgresql://localhost:5432",
dbUser:String = "usuario",
dbPassword:String = "senha"): SessionFactory = {
val cfg:Configuration = new Configuration()
.setProperty("hibernate.connection.driver", dbDriver)
.setProperty("hibernate.dialect", dbDialect)
.setProperty("hibernate.connection.url", dbPath)
.setProperty("hibernate.connection.user", dbUser)
.setProperty("hibernate.connection.password", dbPassword)
.setProperty("hibernate.show_sql", "false")
.setProperty("hibernate.hbm2ddl.auto", "update")
.setProperty("hibernate.c3p0.min_size", "100")
.setProperty("hibernate.c3p0.max_size", "300")
.setProperty("hibernate.c3p0.timeout", "1800")
.setProperty("hibernate.c3p0.max_statements", "100")
.setProperty("hibernate.generate_statistics", "true")
.addAnnotatedClass(classOf[Buddy])
cfg.buildSessionFactory( buildServiceRegistry(cfg) )
}
private def buildServiceRegistry(cfg:Configuration):ServiceRegistry = {
val serviceRegistryBuilder:ServiceRegistryBuilder = new ServiceRegistryBuilder()
serviceRegistryBuilder.applySettings(cfg.getProperties());
serviceRegistryBuilder.buildServiceRegistry()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment