Creating DAO Objects"

From Documentation
(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…')
 
m (correct highlight (via JWB))
 
Line 4: Line 4:
 
For ease of maintenance, we used to create another Java class to handle data accessing jobs.  
 
For ease of maintenance, we used to create another Java class to handle data accessing jobs.  
  
# Create <tt>EventDAO.java</tt>
+
# Create <code>EventDAO.java</code>
  
 
<source lang="java" >
 
<source lang="java" >
Line 40: Line 40:
 
</source>  
 
</source>  
  
# You have to compile the Java source, and place the class file in a directory called <tt>classes</tt> in the Web development folder, and in its correct package. (ex.<tt>$myApp/WEB-INF/classes/event/EventDAO.class</tt>)
+
# You have to compile the Java source, and place the class file in a directory called <code>classes</code> in the Web development folder, and in its correct package. (ex.<code>$myApp/WEB-INF/classes/event/EventDAO.class</code>)
  
 
{{ ZKDevelopersGuidePageFooter}}
 
{{ ZKDevelopersGuidePageFooter}}

Latest revision as of 10:38, 19 January 2022

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.