Creating DAO Objects

From Documentation

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 : 2022/01/19

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