notedaohibernate.java

来自「STRUTS数据库项目开发宝典」· Java 代码 · 共 64 行

JAVA
64 行
字号
package com.relationinfo.dao.hibernate;import java.util.List;import com.relationinfo.model.Note;import com.relationinfo.dao.NoteDAO;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.criterion.Example;import org.hibernate.criterion.MatchMode;import org.springframework.orm.ObjectRetrievalFailureException;import org.springframework.orm.hibernate3.HibernateCallback;public class NoteDAOHibernate extends BaseDAOHibernate implements NoteDAO {    /**     * @see com.relationinfo.dao.NoteDAO#getNotes(com.relationinfo.model.Note)     */    public List getNotes(final Note note) {        if (note == null) {            return getHibernateTemplate().find("from Note");        } else {            // filter on properties set in the note            HibernateCallback callback = new HibernateCallback() {                public Object doInHibernate(Session session) throws HibernateException {                    Example ex = Example.create(note).ignoreCase().enableLike(MatchMode.ANYWHERE);                    return session.createCriteria(Note.class).add(ex).list();                }            };            return (List) getHibernateTemplate().execute(callback);        }    }    /**     * @see com.relationinfo.dao.NoteDAO#getNote(String notecode)     */    public Note getNote(final String notecode) {        Note note = (Note) getHibernateTemplate().get(Note.class, notecode);        if (note == null) {            log.warn("uh oh, note with notecode '" + notecode + "' not found...");            throw new ObjectRetrievalFailureException(Note.class, notecode);        }        return note;    }    /**     * @see com.relationinfo.dao.NoteDAO#saveNote(Note note)     */        public void saveNote(final Note note) {        getHibernateTemplate().saveOrUpdate(note);    }    /**     * @see com.relationinfo.dao.NoteDAO#removeNote(String notecode)     */    public void removeNote(final String notecode) {        getHibernateTemplate().delete(getNote(notecode));    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?