Creating DAO Objects

From Documentation
Revision as of 04:36, 19 July 2010 by Char (talk | contribs) (Created page with '{{ZKDevelopersGuidePageHeader}} For ease of maintenance, we used to create another Java class to handle data accessing jobs. # Create <tt>EventDAO.java</tt> <source lang="ja…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Stop.png This documentation is for an older version of ZK. For the latest one, please click here.



For ease of maintenance, we used to create another Java class to handle data accessing jobs.

  1. Create EventDAO.java
 package events;

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

 import org.hibernate.Session;
 import org.zkoss.zkplus.hibernate.HibernateUtil;

 public class EventDAO {
     Session currentSession() {
         return HibernateUtil.currentSession();
     }
     public void saveOrUpdate(Event anEvent, String title, Date date) {
         Session sess =  currentSession();
         anEvent.setTitle(title);
         anEvent.setDate(date);
         sess.saveOrUpdate(anEvent);
     }
     public void delete(Event anEvent) {
         Session sess =  currentSession();
         sess.delete(anEvent);
     }
     public Event findById(Long id) {
         Session sess =  currentSession();
         return (Event) sess.load(Event.class, id);
     }
     public List findAll() {
         Session sess =  currentSession();
         return sess.createQuery("from Event").list();
     }
 }
  1. You have to compile the Java source, and place the class file in a directory called classes in the Web development folder, and in its correct package. (ex.$myApp/WEB-INF/classes/event/EventDAO.class)



Last Update : 2010/07/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.