UI initial implementation.
[validation.git] / ui / src / main / java / org / akraino / validation / ui / config / HibernateConfig.java
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 (file)
index 0000000..03a91be
--- /dev/null
@@ -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;
+    }
+}