⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eventdao.java

📁 hibernate quickly 一书的各章节源代码
💻 JAVA
字号:
package com.manning.hq.ch10;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Date;
import java.util.List;


/**
 * The Data Access Object for managing the persistent Events.
 *
 * @author Patrick Peak
 * @author Nick Heudecker
 */
public class EventDao extends AbstractDao {
    Log log = LogFactory.getLog(EventDao.class);

    public EventDao() {
        super();
    }

    /**
     * Insert a new Event into the database.
     * @param event
     */
    public void create(Event event) throws DataAcessLayerException {
        super.saveOrUpdate(event);
    }


    /**
     * Delete a detached Event from the database.
     * @param event
     */
    public void delete(Event event) throws DataAcessLayerException {
        super.delete(event);
    }

    /**
     * Find an Event by its primary key.
     * @param id
     * @return
     */
    public Event find(Long id) throws DataAcessLayerException {
        return (Event) super.find(Event.class, id);
    }

    /**
     * Updates the state of a detached Event.
     *
     * @param event
     */
    public void update(Event event) throws DataAcessLayerException {
        super.saveOrUpdate(event);
    }

    /**
     * Finds all Events in the database.
     * @return
     */
    public List findAll() throws DataAcessLayerException{
        return super.findAll(Event.class);
    }
    /**
     * Find all events in the given month and year.
     * @param month - 0-11 (0 = January)
     * @param year
     */
    public List findEventsFor(int month, int year) {
        List events = null;
        try {
            startOperation();
            Date firstDay = DateUtils.newDate(month, 1, year);
            Date lastDay = DateUtils.newDate(month + 1, 1, year);

            String q = "from Event event where event.startDate >= :firstDay and event.startDate < :lastDay";
            Query query = getSession().createQuery(q);

            query.setParameter("firstDay", firstDay);
            query.setParameter("lastDay", lastDay);
            events =  query.list();
            getTx().commit();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(getSession());
        }
        return events;
    }


}

⌨️ 快捷键说明

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