X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=blobdiff_plain;f=ui%2Fsrc%2Fmain%2Fjava%2Forg%2Fakraino%2Fvalidation%2Fui%2Fconfig%2FHibernateConfig.java;fp=ui%2Fsrc%2Fmain%2Fjava%2Forg%2Fakraino%2Fvalidation%2Fui%2Fconfig%2FHibernateConfig.java;h=03a91bed3ed30fddf6bba2c5652bb90336426f69;hb=c5ad3fa5dcff60eb9108ed303806ff28b31a9c09;hp=0000000000000000000000000000000000000000;hpb=e8d760fe817577deae92c372b877507adc100a5f;p=validation.git diff --git a/ui/src/main/java/org/akraino/validation/ui/config/HibernateConfig.java b/ui/src/main/java/org/akraino/validation/ui/config/HibernateConfig.java new file mode 100644 index 0000000..03a91be --- /dev/null +++ b/ui/src/main/java/org/akraino/validation/ui/config/HibernateConfig.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.akraino.validation.ui.config; + +import java.util.Properties; + +import javax.sql.DataSource; + +import org.hibernate.SessionFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.orm.hibernate5.HibernateTransactionManager; +import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@Configuration +@EnableTransactionManagement +@PropertySource(value = {"classpath:hibernate.properties"}) +public class HibernateConfig { + + @Autowired + private Environment env; + + @Autowired + @Bean(name = "sessionFactory") + public SessionFactory getSessionFactory(DataSource dataSource) { + LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource); + + sessionBuilder.scanPackages("org.akraino.validation.ui.entity"); + sessionBuilder.addProperties(hibernateProperties()); + + return sessionBuilder.buildSessionFactory(); + + } + + @Autowired + @Bean(name = "transactionManager") + public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) { + + return new HibernateTransactionManager(sessionFactory); + + } + + private Properties hibernateProperties() { + + Properties properties = new Properties(); + + properties.put("hibernate.dialect", env.getProperty("hibernate.dialect")); + properties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql")); + properties.put("hibernate.c3p0.min_size", env.getProperty("hibernate.c3p0.min_size")); + properties.put("hibernate.c3p0.max_size", env.getProperty("hibernate.c3p0.max_size")); + properties.put("hibernate.c3p0.max_statements", env.getProperty("hibernate.c3p0.max_statements")); + properties.put("hibernate.c3p0.acquire_increment", env.getProperty("hibernate.c3p0.acquire_increment")); + properties.put("hibernate.jdbc.use_streams_for_binary", + env.getProperty("hibernate.jdbc.use_streams_for_binary")); + + return properties; + } +}