2014年11月11日 星期二

HibernateUtil

http://www.mediatek.com.tw/?a=url&k=86218eff&u=aHR0cDovL3NlZ21lbnRmYXVsdC5jb20vYmxvZy93YW5nZGFpLzExOTAwMDAwMDA2NzM1MzI=&t=SGliZXJuYXRlLWNvcmUgNC4zLjYgRmluYWwg6YWN572u5Lit5Ye6546w55qE5LiA5Lqb5bCP6Zeu6aKYIC0gU2VnbWVudC4uLg==&s=aGliZXJuYXRlNC4zLjbphY3nva4=


package com;

import javax.persistence.EntityManagerFactory;

import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {
    private org.hibernate.Transaction tx;
    private Session session;
    private static Boolean isAvaliable = true;

    private static final SessionFactory sessionFactory = buildSessionFactory();

    public HibernateUtil(){
        this.session = sessionFactory.openSession();
    }
    private static SessionFactory buildSessionFactory() {
        try {
            Configuration config = new Configuration();
            String sessionFactoryPath = (String) ServletActionContext.getServletContext().getInitParameter("SessionFactoryPath");
            config.configure(sessionFactoryPath);

            return config.buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build());
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }

    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public String[] getColumnNames(Class<?> calsss) {
        ClassMetadata hibernateMetadata = sessionFactory.getClassMetadata(calsss);
        String[] columnNames = hibernateMetadata.getPropertyNames();

        return columnNames;

    }

    public org.hibernate.type.Type[] getColumnTypes(Class<?> calsss) {

        ClassMetadata hibernateMetadata = sessionFactory.getClassMetadata(calsss);
        org.hibernate.type.Type[] columnTypes = hibernateMetadata.getPropertyTypes();
        return columnTypes;

    }

    public Boolean getIsAvaliable() {
        return isAvaliable;
    }

    public Session getSession() {
        return this.session;
    }

    public int sessionBeginTransaction() {
        try {
            tx = session.beginTransaction();
        } catch (HibernateException e) {
            e.printStackTrace();
            return 0;
        }

        return 1;
    }

    public int commitTransactionAndClose() {
        try {
            this.tx.commit();
            System.err.println("commitTransactionAndClose() sus");
            return 1;
        } catch (HibernateException e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();

            return 0;
        } finally {
            session.close();

        }

    }

    public Transaction getTransaction() {
        return this.tx;
    }

    public void close() {
        try {
            if (tx != null) {
                if (tx.isActive()) {
                    tx.rollback();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {

            if (session != null) {
                if (session.isOpen()) {
                    session.close();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}



20150214
public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            new Configuration().configure().buildSessionFactory(
   new StandardServiceRegistryBuilder().build() );
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
return null;
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

沒有留言:

張貼留言