UI initial implementation.
[validation.git] / ui / src / main / java / org / akraino / validation / ui / config / HibernateConfig.java
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.akraino.validation.ui.config;
17
18 import java.util.Properties;
19
20 import javax.sql.DataSource;
21
22 import org.hibernate.SessionFactory;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.context.annotation.Bean;
25 import org.springframework.context.annotation.Configuration;
26 import org.springframework.context.annotation.PropertySource;
27 import org.springframework.core.env.Environment;
28 import org.springframework.orm.hibernate5.HibernateTransactionManager;
29 import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
30 import org.springframework.transaction.annotation.EnableTransactionManagement;
31
32 @Configuration
33 @EnableTransactionManagement
34 @PropertySource(value = {"classpath:hibernate.properties"})
35 public class HibernateConfig {
36
37     @Autowired
38     private Environment env;
39
40     @Autowired
41     @Bean(name = "sessionFactory")
42     public SessionFactory getSessionFactory(DataSource dataSource) {
43         LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
44
45         sessionBuilder.scanPackages("org.akraino.validation.ui.entity");
46         sessionBuilder.addProperties(hibernateProperties());
47
48         return sessionBuilder.buildSessionFactory();
49
50     }
51
52     @Autowired
53     @Bean(name = "transactionManager")
54     public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
55
56         return new HibernateTransactionManager(sessionFactory);
57
58     }
59
60     private Properties hibernateProperties() {
61
62         Properties properties = new Properties();
63
64         properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
65         properties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
66         properties.put("hibernate.c3p0.min_size", env.getProperty("hibernate.c3p0.min_size"));
67         properties.put("hibernate.c3p0.max_size", env.getProperty("hibernate.c3p0.max_size"));
68         properties.put("hibernate.c3p0.max_statements", env.getProperty("hibernate.c3p0.max_statements"));
69         properties.put("hibernate.c3p0.acquire_increment", env.getProperty("hibernate.c3p0.acquire_increment"));
70         properties.put("hibernate.jdbc.use_streams_for_binary",
71                 env.getProperty("hibernate.jdbc.use_streams_for_binary"));
72
73         return properties;
74     }
75 }