對於通常使用Hibernate的正確方式,有許多不同的示例,教程和評論。所以我希望你看看我的代碼,並確認我正確使用它,或者我錯誤地使用它。
編輯 我單獨使用Hibernate。 Hibernate的4:)使用Hibernate的「正確方法」
public class HibernateUtil {
private static SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
System.out.println("Building new SessionFactory !!");
Configuration configuration = null;
ServiceRegistry serviceRegistry = null;
try{
configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
}catch(Exception e){
e.printStackTrace();
}
return new Configuration().configure().buildSessionFactory(serviceRegistry);
}
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);
}
}
//Since thread unsafe
/**
* thread safe or unsafe ?
* assuming thread safe
* @return
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void tryCloseAll(Session s, Transaction t){
try{
if(t!=null){
if(t.isActive()){
//?
try{
t.commit();
System.out.println("HibernateUtil.tryCloseAll():: Transaction successfully committed.");
}catch(Exception e){
System.out.println("HibernateUtil.tryCloseAll():: Exception thrown while trying to commit a transaction.");
}finally{
t = null;
System.out.println("HibernateUtil.tryCloseAll():: Transaction successfully nullified.");
}
}
}else{
System.out.println("HibernateUtil.tryCloseAll():: The method has received a null transaction");
}
if(s!=null){
if(s.isOpen()){
try{
s.close();
System.out.println("HibernateUtil.tryCloseAll():: Session successfully closed.");
}catch(Exception e){
System.out.println("HibernateUtil.tryCloseAll():: Tried to close session and failed");
}finally{
s = null;
System.out.println("HibernateUtil.tryCloseAll():: Session successfully nullified.");
}
}
}else{
System.out.println("HibernateUtil.tryCloseAll():: The method has received a null session");
}
}catch(Exception e){
}
}
和我的DAO類
public class SuperItemDAO extends MSSQLDAO implements InterfaceSuperItemDAO{
private SessionFactory sessionFactory = null;
public static SuperItemDAO getInstance(){
if(INSTANCE == null){
INSTANCE = new SuperItemDAO();
}
return INSTANCE;
}
SuperItemDAO(){
if(sessionFactory == null){
sessionFactory = HibernateUtil.getSessionFactory();
}
}
public SuperItemVO get(int id){
SuperItemVO sivo = null;
Session session = null;
Transaction transaction = null;
try{
session = sessionFactory.getCurrentSession();
transaction = session.getTransaction();
transaction.begin();
Criteria criteria = session.createCriteria(SuperItemVO.class);
criteria.add(Restrictions.eq("id", id));
sivo = (SuperItemVO)criteria.uniqueResult();
transaction.commit();
}catch(Exception e){e.printStackTrace();}finally{HibernateUtil.tryCloseAll(session, transaction);}
return sivo;
}
你是單獨使用hibernate還是使用spring? – Sikorski
@Sikorski,看看我的編輯。 – Ascendant
'正確的方法'是不要使用休眠:) – jackalope