com.rapid.develop.core.system.dal.SessionFactoryBuilder.java Source code

Java tutorial

Introduction

Here is the source code for com.rapid.develop.core.system.dal.SessionFactoryBuilder.java

Source

/*
 * Copyright (c) 2015-2020, Chen Rui
 *
 * 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 com.rapid.develop.core.system.dal;

import com.rapid.develop.core.system.dal.entity.*;
import com.rapid.develop.core.system.dal.entity.fields.FieldDefination;
import com.rapid.develop.core.configure.AppConfigure;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import java.net.URL;

/**
 * Hibernate Session Factory ?.
 *
 * <p>
 * ?SessionFactory, ?
 * @see #getEntitySessionFactory() SessionFactory</p>
 *
 * 
 * <p>
 * // SessionFactory
 * SessionFactoryBuilder.getInstance().getApplicationSessionFactory()</p>
 *
 * <p>
 * // ?SessionFactory 
 * SessionFactoryBuilder.getInstance().getEntitySessionFactory()
 * </p>
 * @author Rui Chen
 *
 */
public class SessionFactoryBuilder {

    private static SessionFactoryBuilder builder = new SessionFactoryBuilder();

    private SessionFactory entitySessionFactory;

    private SessionFactoryBuilder() {
    }

    public static SessionFactoryBuilder getInstance() {
        return builder;
    }

    /**
     * System.
     *
     * <p>Javassit??</p>
     *
     * @return SessionFactory
     */
    public synchronized SessionFactory getEntitySessionFactory() {
        if (this.entitySessionFactory == null) {
            URL url = SessionFactoryBuilder.class.getResource(AppConfigure.HIBERNATE_CFG_XML);
            Configuration configuration = new Configuration().configure(url);
            AppConfigure appConfigure = AppConfigure.loadConfig();
            for (String key : appConfigure.getConfigurationValues().keySet()) {
                configuration.setProperty(key, appConfigure.getConfigurationValues().get(key));
            }
            configuration.setProperty("hibernate.hbm2ddl.auto", "create");
            configuration.addAnnotatedClass(Application.class);
            configuration.addAnnotatedClass(AppPlugin.class);
            configuration.addAnnotatedClass(AppPluginConfig.class);
            configuration.addAnnotatedClass(AppRestService.class);
            configuration.addAnnotatedClass(AppServiceClass.class);
            configuration.addAnnotatedClass(EntityDefination.class);
            configuration.addAnnotatedClass(FieldDefination.class);
            entitySessionFactory = configuration.buildSessionFactory();
        }
        return this.entitySessionFactory;
    }

}