Summary
This example contains one list box with First name and Last Name as search Field. If the user do not give any values for first name and last name, then clicking Go Button will list all the records from the DB.If the user enters either first name or last name or both, then using like operator, it will retrieve accordingly.
Environment
- Eclipse 3.7 Indigo IDE
- Hibernate 4.1.1
- JavaSE 1.6
- MySQL 5.1
Step 1:
Set up ZK Project with Hibernate. Follow this post for more information.
Step 2:
Create the Following table in mysql
Step 3:
Let us create the Java Bean for the above Table
Here is the Code
package myDomain; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "person") public class Person { private int ID; private String FirstName; private String LastName; private String Email; @Id @GeneratedValue public int getID() { return ID; } public void setID(int iD) { ID = iD; } @Column(name = "FirstName") public String getFirstName() { return FirstName; } public void setFirstName(String firstName) { FirstName = firstName; } @Column(name = "LastName") public String getLastName() { return LastName; } public void setLastName(String lastName) { LastName = lastName; } @Column(name = "Email") public String getEmail() { return Email; } public void setEmail(String email) { Email = email; } }
Step 4:
Go Back to hibernate.cfg.xml and add the above class as follows
<!-- Mapping Classes -->
<mapping class="myDomain.Person" />
Step 5:
Demo. Zul File
<?page title="Person List" contentType="text/html;charset=UTF-8"?> <?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?> <zk> <window id='myWin' title="Person List" border="normal" apply="myUI.DemoComposer"> <groupbox height="40px"> <label value="First Name" /> <space /> <space /> <textbox id="FirstName" cols="50" /> <label value="Last Name" /> <space /> <space /> <textbox id="LastName" cols="50" /> <button id="gobutton" label="Go" /> <space spacing="20px" /> <space spacing="20px" /> </groupbox> <separator /> <listbox id="PersonList" > <listhead sizable=" true "> <listheader label="First Name" sort="auto" /> <listheader label="Last Name" sort="auto" /> <listheader label="Email" sort="auto" /> </listhead> <listitem self="@{each=person}"> <listcell label="@{person.firstName}" /> <listcell label="@{person.LastName}" /> <listcell label="@{person.Email}" /> </listitem> </listbox> </window> </zk>
Step 6:
DemoComposer.Java
package myUI; import java.util.ArrayList; import java.util.List; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.util.GenericForwardComposer; import org.zkoss.zul.ListModel; import org.zkoss.zul.ListModelList; import org.zkoss.zul.Listbox; import org.zkoss.zul.Textbox; import myDomain.Person; import myDomain.PersonDAO; public class DemoComposer extends GenericForwardComposer { private List<Person> persons = new ArrayList<Person>(); private Textbox FirstName; private Textbox LastName; private Listbox PersonList; private PersonDAO p1DAO; @SuppressWarnings("unchecked") public void onClick$gobutton(Event event) { p1DAO = new PersonDAO(); p1DAO.setFirstName(FirstName.getValue()); p1DAO.setLastName(LastName.getValue()); persons = p1DAO.getAllPersons(); PersonList.getItems().clear(); PersonList.setModel(new ListModelList(persons)); } public List<Person> getPersons() { return persons; } }
Step 7:
PersonDAO. Java
Code
package myDomain; import java.util.List; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.Query; import org.zkoss.zul.Messagebox; import HibernateUtilities.HibernateUtil; public class PersonDAO { private String firstName; private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @SuppressWarnings("unchecked") public List<Person> getAllPersons() { List<Person> allrecords = null; Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); if ((firstName == null || "".equals(firstName)) && (lastName == null || "".equals(lastName))) { Query q1 = session.createQuery("from Person "); allrecords = q1.list(); } if ((firstName != null && !firstName.equals("")) && (lastName == null || "".equals(lastName))) { Query q1 = session.createQuery("from Person where firstname like :value "); q1.setString("value", firstName +"%"); allrecords = q1.list(); } if ((lastName != null && !lastName.equals("")) && (firstName == null || "".equals(firstName))) { Query q1 = session.createQuery("from Person where lastname like :value "); q1.setString("value", lastName +"%"); allrecords = q1.list(); } if ((firstName != null && !firstName.equals("")) && (lastName != null && !lastName.equals(""))) { Query q1 = session.createQuery("from Person where firstname like :value1 or lastname like :value2 "); q1.setString("value1", firstName +"%"); q1.setString("value2", lastName +"%"); allrecords = q1.list(); } session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } return allrecords; } }
Step 7:
Run the Demo.ZUL File