Package abstracts

Source Code of abstracts.HibernateFactory

package abstracts;

import org.hibernate.*;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import dao.BucakDAO;
import dao.FirmalarAnaDAO;
import dao.FirmalarSubelerAnaDAO;
import dao.FirmalarSubelerTelefonDAO;
import dao.FirmalarTelefonDAO;
import dao.KontakKisilerAnaDAO;
import dao.KontakKisilerTelefonDAO;
import dao.KosullarDAO;
import dao.OnyazilarDAO;
import dao.SehirDAO;
import dao.SozlesmelerDAO;
import dao.TeslimatSartlariDAO;
import dao.UlkeDAO;
import dao.UrunlerAnaDAO;
import dao.UrunlerExtraDAO;

public class HibernateFactory {
  private static SessionFactory sessionFactory;

  /** Constructs a new Singleton SessionFactory
   *
   * @return
   * @throws HibernateException */
  public static SessionFactory buildSessionFactory() throws HibernateException {
    if (sessionFactory != null) {
      closeFactory();
    }
    return configureSessionFactory();
  }

  /** Builds a SessionFactory, if it hasn't been already. */
  public static SessionFactory buildIfNeeded() throws DataAccessLayerException {
    if (sessionFactory != null) {
      return sessionFactory;
    }
    try {
      return configureSessionFactory();
    } catch (HibernateException e) {
      throw new DataAccessLayerException(e);
    }
  }

  public static SessionFactory getSessionFactory() {
    return sessionFactory;
  }

  public static Session openSession() throws HibernateException {
    buildIfNeeded();
    return sessionFactory.openSession();
  }

  public static void closeFactory() {
    if (sessionFactory != null) {
      try {
        sessionFactory.close();
      } catch (HibernateException e) {
        e.printStackTrace();
      }
    }
  }

  public static void close(Session session) {
    if (session != null) {
      try {
        session.close();
      } catch (HibernateException e) {
        e.printStackTrace();
      }
    }
  }

  public static void rollback(Transaction tx) {
    try {
      if (tx != null) {
        tx.rollback();
      }
    } catch (HibernateException e) {
      e.printStackTrace();
    }
  }

  /** @return
   * @throws HibernateException */
  @SuppressWarnings("deprecation")
  private static SessionFactory configureSessionFactory() throws HibernateException {
   
    AnnotationConfiguration annotationConfiguration = new AnnotationConfiguration();
    annotationConfiguration.addAnnotatedClass(OnyazilarDAO.class);
    annotationConfiguration.addAnnotatedClass(BucakDAO.class);
    annotationConfiguration.addAnnotatedClass(FirmalarAnaDAO.class);
    annotationConfiguration.addAnnotatedClass(FirmalarSubelerAnaDAO.class);
    annotationConfiguration.addAnnotatedClass(FirmalarSubelerTelefonDAO.class);
    annotationConfiguration.addAnnotatedClass(FirmalarTelefonDAO.class);
    annotationConfiguration.addAnnotatedClass(KontakKisilerAnaDAO.class);
    annotationConfiguration.addAnnotatedClass(KontakKisilerTelefonDAO.class);
    annotationConfiguration.addAnnotatedClass(KosullarDAO.class);
    annotationConfiguration.addAnnotatedClass(SehirDAO.class);
    annotationConfiguration.addAnnotatedClass(SozlesmelerDAO.class);
    annotationConfiguration.addAnnotatedClass(TeslimatSartlariDAO.class);
    annotationConfiguration.addAnnotatedClass(UlkeDAO.class);
    annotationConfiguration.addAnnotatedClass(UrunlerAnaDAO.class);
    annotationConfiguration.addAnnotatedClass(UrunlerExtraDAO.class);
    annotationConfiguration.configure("hibernate.cfg.xml");
  //  new SchemaExport(annotationConfiguration).create(true,true);
   
    Configuration configuration = new Configuration();
    configuration.configure();
    sessionFactory = configuration.buildSessionFactory();
    return sessionFactory;
  }
}
TOP

Related Classes of abstracts.HibernateFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.