📄 commentdao.java
字号:
/* * Copyright (c) 2004 Manning Publications. All Rights Reserved. */package org.hibernate.auction.dao;import net.sf.hibernate.Criteria;import net.sf.hibernate.HibernateException;import net.sf.hibernate.LockMode;import net.sf.hibernate.Session;import net.sf.hibernate.expression.Example;import org.hibernate.auction.exceptions.InfrastructureException;import org.hibernate.auction.model.Comment;import java.util.Collection;/** * A typical DAO for comments using Hibernate * * @author Christian Bauer <christian@hibernate.org> */public class CommentDAO extends AbstractDAO { // ********************************************************** // public Comment getCommentById(Long commentId, boolean lock) throws InfrastructureException { Session session = persistenceManager.getSession(); Comment comment = null; try { if (lock) { comment = (Comment) session.load(Comment.class, commentId, LockMode.UPGRADE); } else { comment = (Comment) session.load(Comment.class, commentId); } } catch (HibernateException ex) { throw new InfrastructureException(ex); } return comment; } // ********************************************************** // public Collection findAll() throws InfrastructureException { Collection comments; try { comments = persistenceManager.getSession().createCriteria(Comment.class).list(); } catch (HibernateException ex) { throw new InfrastructureException(ex); } return comments; } // ********************************************************** // public Collection findByExample(Comment exampleComment) throws InfrastructureException { Collection comments; try { Criteria crit = persistenceManager.getSession().createCriteria(Comment.class); comments = crit.add(Example.create(exampleComment)).list(); } catch (HibernateException ex) { throw new InfrastructureException(ex); } return comments; } // ********************************************************** // public void makePersistent(Comment comment) throws InfrastructureException { try { persistenceManager.getSession().saveOrUpdate(comment); } catch (HibernateException ex) { throw new InfrastructureException(ex); } } // ********************************************************** // public void makeTransient(Comment comment) throws InfrastructureException { try { persistenceManager.getSession().delete(comment); } catch (HibernateException ex) { throw new InfrastructureException(ex); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -