📄 hibernatemessagemanager.java
字号:
/*
* HibernateMessageManager.java
*
* Created on 2007年5月28日, 下午7:59
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author abc
*/
package message_board;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
public class HibernateMessageManager implements BoardManager
{
private Connection _connection;
private SessionFactory _sessionFactory;
private Session _currentSession;
public HibernateMessageManager(Connection connection)
{
_connection = connection;
Configuration configuration = new Configuration();
try
{
configuration.addClass(Message.class);
}
catch (MappingException mappingFailure)
{
throw new IllegalArgumentException(
"Cannot find required object classes, check CLASSPATH : " + mappingFailure);
}
Properties props = new Properties();
props.put("hibernate.dialect", "net.sf.hibernate.dialect.MySQLDialect");
props.put("hibernate.statement_cache.size", "0");
configuration.setProperties(props);
try
{
_sessionFactory = configuration.buildSessionFactory();
}
catch (HibernateException sessionFailure)
{
throw new IllegalArgumentException(
"Cannot build session factory, check configuration : " + sessionFailure);
}
}
/*-- getting objects --*/
public List getMessages() throws DataException
{
return getAllItems("from Message as message order by message.id asc");
}
private List getAllItems(String query) throws DataException
{
initSession();
List list = null;
try
{
Query allTeams = _currentSession.createQuery(query);
list = allTeams.list();
}
catch (HibernateException unexpected)
{
throw new DataException(unexpected.getMessage());
}
finally
{
flushSession();
}
return list;
}
/*-- adding and modifying objects --*/
public boolean addNewMessage(Message message) throws DataException
{
return addOrModifyObject(message, "ADD");
}
public boolean modifyMessage(Message message) throws DataException
{
return addOrModifyObject(message, "MODIFY");
}
private boolean addOrModifyObject(Message message, String action) throws DataException
{
initSession();
Transaction tx = null;
try
{
tx = _currentSession.beginTransaction();
_currentSession.saveOrUpdate(message);
tx.commit();
}
catch (HibernateException cannotSaveOrUpdate)
{
try
{
tx.rollback();
}
catch (HibernateException cannotRollback)
{
// log this error
}
throw new DataException("Unable to " + action + " : " + cannotSaveOrUpdate.getMessage());
}
finally
{
flushSession();
}
return false;
}
/*-- delete objects --*/
public boolean deleteMessage(int id) throws DataException
{
initSession();
Transaction tx = null;
try
{
tx = _currentSession.beginTransaction();
Message deleted = (Message)loadObjectById(Message.class,id);
_currentSession.delete(deleted);
tx.commit();
}
catch (HibernateException cannotDelete)
{
try
{
tx.rollback();
}
catch (HibernateException cannotRollback)
{
// log this error
}
throw new DataException("Unable to delete : " + cannotDelete.getMessage());
}
finally
{
flushSession();
}
return false;
}
/*-- utility methods --*/
private Object loadObjectById(Class clazz, int id) throws DataException
{
initSession();
Object rv = null;
try
{
rv = _currentSession.load(clazz, new Integer(id));
}
catch (HibernateException unexpected)
{
throw new DataException(unexpected.getMessage());
}
finally
{
flushSession();
}
return rv;
}
private void initSession()
{
if (_currentSession == null)
{
_currentSession = _sessionFactory.openSession(_connection);
}
}
private void flushSession()
{
try
{
if (_currentSession != null)
{
_currentSession.flush();
}
}
catch (HibernateException sessionFlushFailure)
{
// log this error
}
}
public void releaseResources()
{
if (_connection != null)
{
try
{
if (_currentSession != null)
{
_currentSession.connection().close();
_currentSession.close();
_currentSession = null;
}
_connection.close();
}
catch (HibernateException sessionCloseFailure)
{
// log this error
}
catch (SQLException connectionCloseFailure)
{
// log this error
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -