THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
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 | |||||
---|---|---|---|---|---|
| |||||
<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 | |||||
---|---|---|---|---|---|
| |||||
<!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 | ||||
---|---|---|---|---|
| ||||
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 | ||||
---|---|---|---|---|
| ||||
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 | |||||
---|---|---|---|---|---|
| |||||
<t:grid source="addresses"/>
|
And all we have to do is supply the addresses property in the Java code:
Code Block | ||||
---|---|---|---|---|
| ||||
@Inject
private Session session;
public List<Address> getAddresses()
{
return session.createCriteria(Address.class).list();
}
|
...