...
Code Block | ||||
---|---|---|---|---|
| ||||
<dependencies> <dependency> <groupId>org.apache.tapestry</groupId> <artifactId>tapestry-hibernate</artifactId> <version>${tapestry-release-version}</version> </dependency> <dependency> <groupId>hsqldb<<groupId>org.hsqldb </groupId> <artifactId>hsqldb</artifactId> <version>1<version>2.83.0.7<2</version> </dependency> ... </dependencies> |
...
After changing the POM and saving, Maven should automatically download the JARs for the new dependencies.
Hibernate Configuration
Hibernate has needs a master configuration file, hibernate.cfg.xml, used to store connection and other data. Create this in your src/main/resources folder:
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> |
...
But what entities? Normally, the available entities are listed inside hibernate.cfg.xml, but that's not necessary with Tapestry; in another example of convention over configuration, Tapestry locates all entity classes inside the entities package ("com.example.tutorial.entities" in our case) and adds them to the configuration. Currently, that is just the Address entity.
...
- @NonVisual – indicates a field, such as a primary key, that should not be made visible to the user.
- @Validate – identifies the validations associated with a field.
At this point you should stop and restart your application.
Updating the Database
So we have a database set up and running, and Hibernate is configured to connect to it. Let's make use of that to store our Address object in the database.
What we need is to provide some code to be executed when the form is submitted. When a Tapestry form is submitted, there is a whole series of events that get fired. The event we are interested in is the "success" event, which comes late in the process, after all the values have been pulled out of the request and applied to the page properties, and after all server-side validations have occuredoccurred.
The success event is only fired if there are no validation errors.
...
- Use the Hibernate Session object to persist the new Address object.
- Commit the transaction to force the data to be written to the database.
Let's update our CreateAddress.java class:
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; } } |
...
Tapestry automatically starts a transaction as necessary; however that transaction will be aborted at the end of the request by default. If we make changes to persistent objects, such as adding a new Address object, then it is necessary to commit the transaction.
...