2 * Copyright 2016 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.aai.esr.db.util;
18 import org.hibernate.SessionFactory;
19 import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
20 import org.hibernate.cfg.Configuration;
21 import org.hibernate.service.ServiceRegistry;
24 import java.net.URISyntaxException;
26 public class HibernateSession {
27 private static File cfgfile = null;
29 private static ServiceRegistry serviceRegistry = null;
30 private static Configuration configuration = null;
31 private static SessionFactory sessionFactory = null;
32 private static String resourcePath;
35 * Get a hibernate sessionFactory.
37 public static SessionFactory init() {
39 configuration = new Configuration().configure(cfgfile);
40 configuration.setProperty("hibernate.connection.url",
41 "jdbc:h2:tcp://localhost:18207/" + resourcePath + "db/extsys");
43 new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
44 sessionFactory = configuration.buildSessionFactory(serviceRegistry);
45 return sessionFactory;
48 private static void initConfigure() {
50 resourcePath = HibernateSession.class.getResource("/").toURI().getPath();
51 } catch (URISyntaxException error) {
52 error.printStackTrace();
54 final String filename = "Hibernate.cfg.xml";
55 cfgfile = new File(resourcePath + filename);
59 * Destory a hibernate sessionFactory.
61 public static void destory() {
62 if (sessionFactory != null && !sessionFactory.isClosed()) {
63 sessionFactory.close();