Java tutorial
/* * Copyright Bruce Liang (ldcsaa@gmail.com) * * Version : JessMA 3.5.1 * Author : Bruce Liang * Website : http://www.jessma.org * Project : http://www.oschina.net/p/portal-basic * Blog : http://www.cnblogs.com/ldcsaa * WeiBo : http://weibo.com/u/1402935851 * QQ Group : 75375912 * * 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 org.jessma.dao.hbn; import java.security.InvalidParameterException; import java.sql.Connection; import java.sql.SQLException; import java.util.Set; import javax.persistence.Entity; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.jdbc.Work; import org.jessma.dao.AbstractSessionMgr; import org.jessma.dao.SessionMgr; import org.jessma.dao.TransIsoLevel; import org.jessma.util.BeanHelper; import org.jessma.util.GeneralHelper; import org.jessma.util.PackageHelper; import org.jessma.util.PackageHelper.ClassFilter; /** * * @author Kingfisher * * ?SessionTransaction?? * 1? Hibernate ? * 2? / Session * 3? Session Transaction ? * * ?? InvalidParameterException * * * 1??????? / Session * 2??? Session Transaction ? * 3???? initialize(...)?? * */ public class HibernateSessionMgr extends AbstractSessionMgr<Session> { /** Hibernate ? */ public static final String DEFAULT_CONFIG_FILE = "hibernate.cfg.xml"; /** Session Factory */ private SessionFactory sessionFactory; /** ??? */ private String pattern; /** * ? * * @param args <br> [0] : hbn_cfg_file {@link HibernateSessionMgr#DEFAULT_CONFIG_FILE} <br> * [1] : pattern ????? {@linkplain Entity @Entity} * @throws InvalidParameterException * @throws HibernateException * */ @Override public void initialize(String... args) { if (args.length == 0) initialize(); else if (args.length == 1) initialize(args[0]); else if (args.length == 2) initialize(args[0], args[1]); else throw new InvalidParameterException("HibernateSessionMgr initialize fail (invalid paramers)"); } /** * * Hibernate ?? {@link SessionFactory} * * @throws HibernateException : ? * */ public void initialize() { initialize(DEFAULT_CONFIG_FILE); } /** * * Hibernate ?? {@link SessionFactory} * * @param hbn_cfg_file : ? * @throws HibernateException : ? * */ public void initialize(String hbn_cfg_file) { initialize(hbn_cfg_file, null); } /** * * Hibernate ?? {@link SessionFactory}?? * * @param hbn_cfg_file : ? * @param packages : * @throws HibernateException : ? * */ public void initialize(String hbn_cfg_file, String packages) { configFile = GeneralHelper.isStrNotEmpty(hbn_cfg_file) ? hbn_cfg_file : DEFAULT_CONFIG_FILE; pattern = packages; try { loadDefalutTransIsoLevel(); } catch (HibernateException e) { unInitialize(); throw e; } } /** * * {@link SessionFactory} * * @throws HibernateException : ? * */ @Override public void unInitialize() { if (sessionFactory != null) { synchronized (this) { if (sessionFactory != null) { sessionFactory.close(); sessionFactory = null; super.unInitialize(); } } } } /** ?{@link AbstractSessionMgr#loadDefalutTransIsoLevel()} */ @Override protected void loadDefalutTransIsoLevel() { try { Session session = getSession(); session.doWork(new Work() { @Override public void execute(Connection connection) throws SQLException { int level = connection.getTransactionIsolation(); defaultTransIsoLevel = TransIsoLevel.fromInt(level); } }); } finally { closeSession(); } } /** ?{@link SessionMgr#setSessionTransIsoLevel(TransIsoLevel)} */ @Override public void setSessionTransIsoLevel(final TransIsoLevel level) { Session session = getSession(); session.doWork(new Work() { @Override public void execute(Connection connection) throws SQLException { connection.setTransactionIsolation(level.toInt()); } }); } /** * * ?? * */ @Override public String getConfigFile() { return configFile; } /** * * ?? {@link SessionFactory} * */ public final SessionFactory getSessionFactory() { return sessionFactory; } /** * * ?? {@link Session} * * @throws HibernateException : ? {@link Session} * */ @Override public final Session getSession() { Session session = localSession.get(); if (session == null || !session.isOpen()) { if (sessionFactory == null) { synchronized (this) { if (sessionFactory == null) sessionFactory = buildSessionFactory(); } } session = sessionFactory.openSession(); localSession.set(session); } return session; } // Hibernate 4.x ? @SuppressWarnings("deprecation") protected SessionFactory buildSessionFactory() { Configuration cfg = new Configuration(); if (GeneralHelper.isStrNotEmpty(pattern)) { Set<String> packages = PackageHelper.getPackages(pattern); for (String pkg : packages) { Set<Class<?>> entities = PackageHelper.getClasses(pkg, false, new ClassFilter() { @Override public boolean accept(Class<?> clazz) { if (!BeanHelper.isPublicNotAbstractClass(clazz)) return false; if (clazz.getAnnotation(Entity.class) == null) return false; return true; } }); for (Class<?> clazz : entities) cfg.addAnnotatedClass(clazz); } } return cfg.configure(configFile).buildSessionFactory(); } /** * * ? {@link Session} * */ @Override public final void closeSession() { Session session = localSession.get(); localSession.set(null); if (session != null) { session.close(); } } /** * * ? {@link Session} {@link Transaction} * */ public final Transaction getTransaction() { Session session = getSession(); return session != null ? session.getTransaction() : null; } /** * * * */ @Override public final void beginTransaction() { Transaction trans = getTransaction(); if (trans != null) trans.begin(); } /** * * ?? * */ @Override public final void commit() { Transaction trans = getTransaction(); if (trans != null) trans.commit(); } /** * * * */ @Override public final void rollback() { Transaction trans = getTransaction(); if (trans != null) trans.rollback(); } }