本文整理汇总了Java中org.hibernate.SessionFactory.getCurrentSession方法的典型用法代码示例。如果您正苦于以下问题:Java SessionFactory.getCurrentSession方法的具体用法?Java SessionFactory.getCurrentSession怎么用?Java SessionFactory.getCurrentSession使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.hibernate.SessionFactory
的用法示例。
在下文中一共展示了SessionFactory.getCurrentSession方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getUserByUsername
import org.hibernate.SessionFactory; //导入方法依赖的package包/类
public User getUserByUsername(String name) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();
try
{
User user = null;
session.beginTransaction();
Criteria crit = session.createCriteria(User.class);
crit.add(Restrictions.eq("username",name));
List<User> resultList = crit.list();
if (resultList.size() > 0) {
user = (User) crit.list().get(0);
}
session.getTransaction().commit();
return user;
}
catch (HibernateException e)
{
System.out.println("Hibernate Exception" + e.getMessage());
session.getTransaction().rollback();
throw new RuntimeException(e);
}
}
示例2: doFilter
import org.hibernate.SessionFactory; //导入方法依赖的package包/类
public Object doFilter(Object object, Method method, Object[] params, AjaxFilterChain chain) throws Exception
{
ServletContext context = WebContextFactory.get().getServletContext();
SessionFactory sessionFactory = (SessionFactory) context.getAttribute(ATTRIBUTE_SESSION);
Transaction transaction = null;
if (sessionFactory != null)
{
Session session = sessionFactory.getCurrentSession();
transaction = session.beginTransaction();
}
else
{
log.error("SessionFactory not initialized for this web application. Use: H3SessionAjaxFilter.setSessionFactory(servletContext, sessionFactory);");
}
Object reply = chain.doFilter(object, method, params);
if (transaction != null)
{
transaction.commit();
}
return reply;
}
示例3: getNewsInfoByConditionAndPage
import org.hibernate.SessionFactory; //导入方法依赖的package包/类
/**
* 根据条件查询指定新闻
*/
@Override
public List<?> getNewsInfoByConditionAndPage(NewsInfo condition, int page, int pageSize) {
Session session = SessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(NewsInfo.class);
if (condition != null) {
if (condition.getTopic() != null && condition.getTopic().getId() != null ) {
criteria.add(Restrictions.eq("topic.id", condition.getTopic().getId()));
}
if (condition.getTitle() != null && !"".equals(condition.getTitle())) {
criteria.add(Restrictions.like("title", condition.getTitle(), MatchMode.ANYWHERE));
}
}
criteria.setFirstResult(pageSize * (page - 1));
criteria.setMaxResults(pageSize);
criteria.addOrder(Order.desc("createDate"));
return criteria.list();
}
示例4: getCurrentSession
import org.hibernate.SessionFactory; //导入方法依赖的package包/类
/**
* Get access to a Session, given the {@link SessionFactory} linked in
* {@link #setSessionFactory(ServletContext, SessionFactory)}
* @param context The webapp to link the calls together
* @return A Session from the {@link SessionFactory} or null if
* {@link #setSessionFactory(ServletContext, SessionFactory)} has not been
* called for this {@link ServletContext}
*/
public static Session getCurrentSession(ServletContext context)
{
SessionFactory sessionFactory = (SessionFactory) context.getAttribute(ATTRIBUTE_SESSION);
if (sessionFactory == null)
{
return null;
}
return sessionFactory.getCurrentSession();
}
示例5: listCriteria
import org.hibernate.SessionFactory; //导入方法依赖的package包/类
@SuppressWarnings ("unchecked")
public List<T> listCriteria (DetachedCriteria detached, int skip, int top)
{
SessionFactory factory = getSessionFactory ();
org.hibernate.classic.Session session = factory.getCurrentSession ();
Criteria criteria = detached.getExecutableCriteria (session);
if (skip > 0)
criteria.setFirstResult (skip);
if (top > 0)
criteria.setMaxResults (top);
return criteria.list ();
}
示例6: getAllNewsInfoByPage
import org.hibernate.SessionFactory; //导入方法依赖的package包/类
/**
* 获得指定页码page的列表,列表记录为pageSize
*/
@Override
public List<?> getAllNewsInfoByPage(int page, int pageSize) {
Session session = SessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(NewsInfo.class);
criteria.setFirstResult(pageSize * (page - 1));
criteria.setMaxResults(pageSize);
criteria.addOrder(Order.desc("createDate"));
return criteria.list();
}
示例7: getCountOfAllNewsInfo
import org.hibernate.SessionFactory; //导入方法依赖的package包/类
/**
* 获得记录总数
*/
@Override
public Integer getCountOfAllNewsInfo() {
Session session = SessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(NewsInfo.class);
return criteria.list().size();
}
示例8: getCountOfNewsInfo
import org.hibernate.SessionFactory; //导入方法依赖的package包/类
/**
* 获得指定条件查询得到的记录总数
*/
@Override
public Integer getCountOfNewsInfo(NewsInfo condition) {
Session session = SessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(NewsInfo.class);
if (condition != null) {
if (condition.getTopic() != null && condition.getTopic().getId() != null ) {
criteria.add(Restrictions.eq("topic.id", condition.getTopic().getId()));
}
if (condition.getTitle() != null && !"".equals(condition.getTitle())) {
criteria.add(Restrictions.like("title", condition.getTitle(), MatchMode.ANYWHERE));
}
}
return criteria.list().size();
}
示例9: getNewsInfoById
import org.hibernate.SessionFactory; //导入方法依赖的package包/类
/**
* 根据id取得记录
*/
@Override
public NewsInfo getNewsInfoById(int id) {
Session session = SessionFactory.getCurrentSession();
NewsInfo newsInfo = (NewsInfo) session.get(NewsInfo.class, id);
return newsInfo;
}
示例10: _get_session
import org.hibernate.SessionFactory; //导入方法依赖的package包/类
protected Session _get_session( String _db ) {
if ( sessionFactoryMap.containsKey( _db ) ) {
SessionFactory sessFactory = sessionFactoryMap.get( _db );
if( sessFactory.isClosed()) return sessFactory.openSession();
return sessFactory.getCurrentSession();
}
throw new NullPointerException( "没有找到数据库[" + _db + "]的对应Session容器,请检查配置文件中的[ " + DEFAULT_SFMAP + " ]是否正确。" );
}
示例11: addNews
import org.hibernate.SessionFactory; //导入方法依赖的package包/类
/**
* 增加一条记录
*/
@Override
public void addNews(NewsInfo newsInfo) {
Session session = SessionFactory.getCurrentSession();
session.saveOrUpdate(newsInfo);
}
示例12: updateNews
import org.hibernate.SessionFactory; //导入方法依赖的package包/类
/**
* 更新一条记录
*/
@Override
public void updateNews(NewsInfo newsInfo) {
Session session = SessionFactory.getCurrentSession();
session.saveOrUpdate(newsInfo);
}
示例13: deleteNews
import org.hibernate.SessionFactory; //导入方法依赖的package包/类
/**
* 删除一条记录
*/
@Override
public void deleteNews(NewsInfo newsInfo) {
Session session = SessionFactory.getCurrentSession();
session.delete(newsInfo);
}