hibernate使用注解时的工具类

来源:互联网 发布:五笔 知乎 编辑:程序博客网 时间:2024/06/08 06:20

package com.sun.java.utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

/**
 * @author Dell
 * hibernate使用注解时的工具类
 *
 */
public class HibernateAnnoaUtils {

 private static SessionFactory factory;
 // 静态代码块
 static {
  Configuration cfg = new AnnotationConfiguration().configure();
  factory = cfg.buildSessionFactory();
 }

 // 获取SessionFactory的方法
 public SessionFactory getSessionFactory() {
  return factory;

 }

 // 获取session的方法
 public Session getSession() {
  return factory.openSession();

 }

 // 私有化构造函数
 private HibernateAnnoaUtils() {

 }
}

原创粉丝点击