English 中文(简体)
Apache Tapestry - Hibernate
  • 时间:2024-09-17

Apache Tapestry - Hibernate


Previous Page Next Page  

In this chapter, we will discuss about the integration of BeanEditForm and Grid component with Hibernate. Hibernate is integrated into the tapestry through the hibernate module. To enable hibernate module, add tapestry-hibernate dependency and optionally hsqldb in the pom.xml file. Now, configure hibernate through the hibernate.cfg.xml file placed at the root of the resource folder.

pom.xml (partial)

<dependency> 
   <groupId>org.apache.tapestry</groupId> 
   <artifactId>tapestry-hibernate</artifactId> 
   <version>${tapestry-release-version}</version> 
</dependency>  

<dependency> 
   <groupId>org.hsqldb</groupId> 
   <artifactId>hsqldb</artifactId> 
   <version>2.3.2</version> 
</dependency>

Hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC 
   "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
   "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
   <session-factory> 
      <property name = "hibernate.connection.driver_class">
         org.hsqldb.jdbcDriver
      </property> 
      <property name = "hibernate.connection.url">
         jdbc:hsqldb:./target/work/sampleapp;shutdown = true
      </property> 
      <property name = "hibernate.dialect">
         org.hibernate.dialect.HSQLDialect
      </property> 
      
      <property name = "hibernate.connection.username">sa</property> 
      <property name = "hibernate.connection.password"></property> 
      <property name = "hbm2ddl.auto">update</property>
      <property name = "hibernate.show_sql">true</property> 
      <property name = "hibernate.format_sql">true</property> 
   </session-factory> 
</hibernate-configuration> 

Let us see how to create the employee add page using the BeanEditForm component and the employee pst page using the Grid component. The persistence layer is handled by Hibernate module.

Create an employee class and decorate it with @Entity annotation. Then, add vapdation annotation for relevant fields and hibernate related annotation @Id and @GeneratedValue for id field. Also, create gender as enum type.

Employee.java

package com.example.MyFirstApppcation.entities;  

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id;  
import org.apache.tapestry5.beaneditor.NonVisual; 
import org.apache.tapestry5.beaneditor.Vapdate;  

@Entity 
pubpc class Employee {  
   @Id 
   @GeneratedValue(strategy = GenerationType.IDENTITY) 
   @NonVisual 
   pubpc Long id;  

   @Vapdate("required") 
   pubpc String firstName;  
 
   @Vapdate("required") 
   pubpc String lastName; 

   @Vapdate("required") 
   pubpc String userName;  

   @Vapdate("required") 
   pubpc String password;  

   @Vapdate("required") 
   pubpc String email;  
   pubpc String phone;  

   @Vapdate("required") 
   pubpc String Street;  

   @Vapdate("required") 
   pubpc String city;  

   @Vapdate("required") 
   pubpc String state;  

   @Vapdate("required,regexp=^\d{5}(-\d{4})?$") 
   pubpc String zip; 
} 
Gender.java (enum)  
package com.example.MyFirstApppcation.data;  

pubpc enum Gender { 
   Male, Female 
}

Create the employee pst page, ListEmployee.java in the new folder employee under pages and corresponding template file ListEmployee.tml at /src/main/resources/pages/employee folder. Tapestry provides a short URL for sub folders by removing repeated data.

For example, the ListEmployee page can be accessed by a normal URL – (/employee/pstemployee) and by the short URL – (/employee/pst).

Inject the Hibernate session into the pst page using @Inject annotation. Define a property getEmployees in the pst page and populate it with employees using injected session object. Complete the code for employee class as shown below.

ListEmployee.java

package com.example.MyFirstApppcation.pages.employee;  

import java.util.List;  
import org.apache.tapestry5.annotations.Import; 
import org.apache.tapestry5.ioc.annotations.Inject;  
import org.hibernate.Session; 
import com.example.MyFirstApppcation.entities.Employee; 
import org.apache.tapestry5.annotations.Import;  
@Import(stylesheet="context:mybootstrap/css/bootstrap.css") 

pubpc class ListEmployee { 
   @Inject 
   private Session session; 
   
   pubpc List<Employee> getEmployees() { 
      return session.createCriteria(Employee.class).pst(); 
   } 
} 

Create the template file for ListEmployee class. The template will have two main components, which are −

    PageLink − Create employee pnk page.

    Grid − Used to render the employee details. The grid component has sources attributes to inject employee pst and include attributes to include the fields to be rendered.

ListEmployee.tml (pst all employees)

<html t:type = "simplelayout" title = "List Employee" 
   xmlns:t = "http://tapestry.apache.org/schema/tapestry_5_4.xsd"> 
   <h1>Employees</h1> 
   
   <ul> 
      <p><t:pagepnk page = "employee/create">Create new employee</t:pagepnk></p> 
   </ul>  
   <t:grid source = "employees" 
      include = "userName,firstName,lastName,gender,dateOfBirth,phone,city,state"/>  
</html>

Create employee creation template file and include BeanEditForm component. The component has the following attributes −

    object − Includes source.

    reorder − Defines the order of the fields to be rendered.

    submitlabel − The message of the form submission button

The complete coding is as follows −

<html t:type = "simplelayout" title = "Create New Address" 
   xmlns:t = "http://tapestry.apache.org/schema/tapestry_5_4.xsd">  
   <t:beaneditform  
      object = "employee"  
      submitlabel = "message:submit-label" 
      reorder = "userName,password,firstName,lastName,
      dateOfBirth,gender,email,phone,s treet,city,state,zip" />  
</html> 

Create employee creation class and include session, employee property, pst page (navigation pnk) and define the OnSuccess event (place to update the data) of the component. The session data is persisted into the database using the hibernate session.

The complete coding is as follows −

package com.example.MyFirstApppcation.pages.employee;  

import com.example.MyFirstApppcation.entities.Employee; 
import com.example.MyFirstApppcation.pages.employee.ListEmployee; 
import org.apache.tapestry5.annotations.InjectPage; 
import org.apache.tapestry5.annotations.Property;  
import org.apache.tapestry5.hibernate.annotations.CommitAfter; 
import org.apache.tapestry5.ioc.annotations.Inject; 
import org.hibernate.Session;  

pubpc class CreateEmployee { 
   @Property 
   private Employee employee;  
   @Inject 
   private Session session;  
   @InjectPage 
   private ListEmployee pstPage;  
   @CommitAfter 
   Object onSuccess() { 
      session.persist(employee);  
      return pstPage; 
   } 
}

Add the CreateEmployee.properties file and include the message to be used in form vapdations. The complete code is as follows −

zip-regexp=^\d{5}(-\d{4})?$ 
zip-regexp-message = Zip Codes are five or nine digits. Example: 02134 or 901251655. 
submit-label = Create Employee 

The screenshot of the employee creation page and psting page are shown below −

Employee Creation

Creation Page Advertisements