Creating DAO Objects
From Documentation
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.
- 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();
}
}
- 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
)