Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: fixed language param of code macro

...

First, we must update the POM to list a new set of dependencies, that includes Hibernate, the Tapestry/Hibernate integration library, and the HSQLDB JDBC driver:

Code Block
languagexmlXML
titlesrc/pom.xml (partial)XML

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

        <dependency>
            <groupId>hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>1.8.0.7</version>
        </dependency>

    </dependencies>

...

Hibernate has a master configuration file used to store connection and other data.

Code Block
languageXMLxml
titlesrc/main/resources/hibernate.cfg.xmlXML

<!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/t5_tutorial1;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>

...

Below is the updated Address class, with the Hibernate annotations (as well as the Tapestry ones).

Code Block
languagejava
titlesrc/main/java/com/example/tutorial/entities/Address.java

package com.example.tutorial.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.Validate;

import com.example.tutorial.data.Honorific;

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

  public Honorific honorific;

  @Validate("required")
  public String firstName;

  @Validate("required")
  public String lastName;

  public String street1;

  public String street2;

  @Validate("required")
  public String city;

  @Validate("required")
  public String state;

  @Validate("required,regexp")
  public String zip;

  public String email;

  public String phone;
}

...

  • Use the Hibernate Session object to persist the new Address object.
  • Commit the transaction to force the data to be written to the database.
Code Block
languagejava
titlesrc/main/java/com/example/tutorial/pages/address/CreateAddress.java

package com.example.tutorial.pages.address;

import com.example.tutorial.entities.Address;
import com.example.tutorial.pages.Index;
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;

public class CreateAddress
{
    @Property
    private Address address;

    @Inject
    private Session session;

    @InjectPage
    private Index index;

    @CommitAfter
    Object onSuccess()
    {
        session.persist(address);

        return index;
    }
}

...

A minimal Grid is very easy to add to the template:

Code Block
languageXMLxml
titlesrc/main/webapp/Index.tml (partial)XML

  <t:grid source="addresses"/>

And all we have to do is supply the addresses property in the Java code:

Code Block
languagejava
titlesrc/main/java/com/example/tutorial/pages/Index.java (partial)

    @Inject
    private Session session;

    public List<Address> getAddresses()
    {
        return session.createCriteria(Address.class).list();
    }

...