com.leqienglish.util.SessionBulder.java Source code

Java tutorial

Introduction

Here is the source code for com.leqienglish.util.SessionBulder.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.leqienglish.util;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author guona
 */
public class SessionBulder {

    private static SessionBulder instance = new SessionBulder();

    private static SessionFactory sessionFactory;

    private SessionBulder() {

    }

    /**
     * ? Session
     * @return 
     */
    private static Session getSession() {
        if (sessionFactory == null) {
            // Configuration--?Hibernate.properties
            Configuration configuration = new Configuration();
            // --?Xxx.hbm.xml
            configuration.configure();

            // SessionFactory
            sessionFactory = configuration.buildSessionFactory();
        }
        return sessionFactory.openSession();
    }

    /**
     * HSQL
     *
     * @param hquery
     * @return
     */
    public static List query(String hquery) {
        Session session = getSession();
        if (session == null) {
            return null;
        }
        Query query = session.createQuery(hquery);
        session.close();
        return query.list();
    }
}