Created
July 6, 2013 21:24
-
-
Save agnaldo4j/5941364 to your computer and use it in GitHub Desktop.
Exemplo de criação de SessionFactory com Scala e Hibernate 4.2.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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