This is a very simple tutorial explaining the use of Tapestry-Hibernate module. You can find the official documentation here:http://tapestry.apache.org/tapestry5/tapestry-hibernate/conf.html
you can download example project (tested in Eclipse with mvneclipse) hb.zip

First I will assume that you have properly set up Tapestry, Hibernate-core, Hibernate-Annotation, MySQL in your system.

Following are steps:

Step 1:

Create a working Tapestry project, you can follow the examples in the http://tapestry.apache.org/tapestry5/tutorial1/first.html or simply does the following:

mvn archetype:create  -DarchetypeGroupId=org.apache.tapestry -DarchetypeArtifactId=quickstart  -DarchetypeVersion=5.0.5 -DgroupId=org.example -DartifactId=hb -DpackageName=org.example.hb       

cd hb

mvn jetty:run

open http://localhost:8080/hb/ in your browser, if you see 'hb Start Page' then proceed to step 2 otherwise fix the problems first. (if you want another port use: mvn -Djetty.port=8080 jetty:run )

Step 2:

Switch to an IDE and import the maven project, if you are using Intellij you can use following command: mvn idea:idea

open the project under your IDE and create a package under org.example.hb, name it 'entities', then create a Hello.java under it, copy the following source into Hello.java

Hello.java

package org.example.hb.entities;

import javax.persistence.*;

@Entity
@Table(name="hello")
public class Hello {

    @Id
    @GeneratedValue
    private long id;
    private String message;

    public long getId() { return id;}

    private void setId(long id) { this.id = id; }

    public String getMessage() { return message;}

    public void setMessage(String message) { this.message = message;}
}  

We can't compile this program yet as we need those hibernate's jar files.

Step 3:

a)
This procedure depends on IDE used, the main goal is to make hibernate jar file available to our project, in my case I create a hibernateLib directory which contains following jar files:

        antlr-2.7.6.jar                         dom4j-1.6.1.jar                         jta.jar
        asm.jar                                 ejb3-persistence.jar                    junit-3.8.1.jar
        c3p0-0.9.1.jar                          hibernate-annotations.jar               log4j-1.2.9.jar
        cglib-2.1.3.jar                         hibernate-commons-annotations.jar       tapestry-core-5.0.5.jar
        commons-codec-1.3.jar                   hibernate3.jar                          tapestry-hibernate-5.0.5.jar
        commons-collections-2.1.1.jar           hsqldb.jar                              tapestry-ioc-5.0.5.jar
        commons-logging-1.0.4.jar               javassist-3.4.ga.jar                    testng-5.1-jdk15.jar      

b)
if maven is integrated int your IDE (mvneclipse...) just add this to your pom

    <dependency>
      <groupId>org.apache.tapestry</groupId>
      <artifactId>tapestry-hibernate</artifactId>
      <version>${tapestry-release-version}</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate</artifactId>
      <version>3.2.2.ga</version>
    </dependency>
        <dependency>
                <groupId>c3p0</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.0</version>
        </dependency>
    <dependency>
      <groupId>geronimo-spec</groupId>
      <artifactId>geronimo-spec-jta</artifactId>
      <version>1.0-M1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-annotations</artifactId>
      <version>3.2.1.ga</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>3.1.12</version>
    </dependency>

Step 4:

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">com.mysql.jdbc.Driver</property>
                <property name="hibernate.connection.url">jdbc:mysql://localhost/hb</property>
                <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
                <property name="hibernate.connection.username">root</property>
                <property name="hibernate.connection.password"></property>
                <!-- comment following line during 2nd run -->
               <property name="hbm2ddl.auto">create</property>          

                  <!-- pool via c3p0 which knows how to reconnect to server and does it nicely--> 
            <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
            <property name="hibernate.c3p0.acquire_increment">1</property>
            <property name="hibernate.c3p0.idle_test_period">100</property> <!-- seconds -->
            <property name="hibernate.c3p0.max_size">10</property>
            <property name="hibernate.c3p0.max_statements">0</property>
            <property name="hibernate.c3p0.min_size">1</property>
            <property name="hibernate.c3p0.timeout">100</property> <!-- seconds -->

            </session-factory>
        </hibernate-configuration>

Step 5:

HbAction.java

package org.example.hb.pages;

import java.util.List;

import org.apache.tapestry.annotations.Inject;
import org.example.hb.entities.Hello;
import org.hibernate.Session;

public class HbAction {

    @Inject
    private Session _session;

    private Hello   current;

    @CommitAfter
    public void onAction() {
        Hello h = new Hello();
        h.setMessage("Hello World");
        _session.save(h);
    }

    public List getList() {
        return _session.createCriteria(Hello.class).list();
    }

    public Hello getCurrent() {
        return current;
    }

    public void setCurrent(Hello current) {
        this.current = current;
    }

}

Also add HBAction.htm in webapp/WEB-INF/

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
    <head>
        <title>Hibernate example app</title>
    </head>
    <body>
    <t:loop source="list" value="current">
    ${current.message}<br/>
    </t:loop>
    <t:ActionLink>add</t:ActionLink>
    </body>
</html>

Credits:

Few more advanced things to do

After this basic setup, we can create a very simple form (BeanEditform is explained at http://tapestry.apache.org/tapestry5/tutorial1/forms.html)

Updates

10/4/2008, added @CommitAfter on the onAction method. Without it, a commit never occurs and the new Hello object is never persisted.

Tapestry5HowToUseTapestryHibernate (last edited 2009-09-20 23:20:45 by localhost)