?? hibernateprovider.java
字號:
/*
* Copyright (C) 2003 Erik Swenson - eswenson@opensourcesoft.net
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
package org.efs.openreports.providers.persistence;
import java.io.Serializable;
import java.util.List;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.interceptor.component.ComponentManager;
import net.sf.hibernate.*;
import org.efs.openreports.providers.ProviderException;
public class HibernateProvider implements SessionFactoryProviderAware
{
private SessionFactoryProvider sessionFactoryProvider;
public HibernateProvider() throws ProviderException
{
try
{
ComponentManager container =
(ComponentManager) ActionContext.getContext().get(
"com.opensymphony.xwork.interceptor.component.ComponentManager");
container.initializeObject(this);
}
catch (Exception e)
{
throw new ProviderException("Unable to create HibernateProvider");
}
}
public Session openSession() throws ProviderException
{
try
{
return sessionFactoryProvider.getSessionFactory().openSession();
}
catch (HibernateException he)
{
throw new ProviderException(he);
}
}
public void closeSession(Session session) throws ProviderException
{
try
{
if (session != null)
session.close();
}
catch (HibernateException he)
{
throw new ProviderException(he);
}
}
public void rollbackTransaction(Transaction tx)
throws ProviderException
{
try
{
if (tx != null)
tx.rollback();
}
catch (HibernateException he)
{
throw new ProviderException(he);
}
}
public Object save(Object object) throws ProviderException
{
Session session = openSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
session.save(object);
tx.commit();
}
catch (HibernateException he)
{
rollbackTransaction(tx);
throw new ProviderException(he);
}
finally
{
closeSession(session);
}
return object;
}
public void update(Object object) throws ProviderException
{
Session session = openSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
session.update(object);
tx.commit();
}
catch (HibernateException he)
{
rollbackTransaction(tx);
throw new ProviderException(he);
}
finally
{
closeSession(session);
}
}
public void delete(Object object)
throws ProviderException, ConstraintException
{
Session session = openSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
session.delete(object);
tx.commit();
}
catch (HibernateException he)
{
rollbackTransaction(tx);
if (he.getCause().getMessage().toUpperCase().indexOf("CONSTRAINT") > 0)
{
throw new ConstraintException(he.getMessage());
}
throw new ProviderException(he);
}
finally
{
closeSession(session);
}
}
public Object load(Class classType, Serializable id)
throws ProviderException
{
Session session = openSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
Object object = session.load(classType, id);
tx.commit();
return object;
}
catch (HibernateException he)
{
rollbackTransaction(tx);
throw new ProviderException(he);
}
finally
{
closeSession(session);
}
}
public List query(String fromClause) throws ProviderException
{
Session session = openSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
Query query = session.createQuery(fromClause);
List list = query.list();
tx.commit();
return list;
}
catch (HibernateException he)
{
rollbackTransaction(tx);
throw new ProviderException(he);
}
finally
{
closeSession(session);
}
}
public void setSessionFactoryProvider(SessionFactoryProvider sessionFactoryProvider)
{
this.sessionFactoryProvider = sessionFactoryProvider;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -