Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Fix "Related Articles" box

This page describes functionality provided by the Tapestry-hibernate-core module, but the descriptions apply equally to the Tapestry-jpa module.{

Div
stylefloat:right

...

titleRelated Articles
classaui-label
Content by Label
showLabelsfalse
showSpacefalse
titleRelated Articles
cqllabel = "hibernate" and space = currentSpace()

Entity value encoding

The Tapestry-hibernate-core module provides Value Encoder automatically for all mapped Hibernate entity types. This is done by encoding the entity as it's id (coerced to a String) and decoding the entity by looking it up in the Hibernate Session using the encoded id. Consider the following example:

...

:

...

Accessing the page as /viewperson/152 would load the Person entity with id 152 and use that as the page context.

...

If you prefer to use annotations, you may let Tapestry generate the page activation context handlers for you. Relying on an existing ValueEncoder for the corresponding property you can use the @PageActivationContext annotation. The disadvantage is that you can't access the handlers in a unit test.

...

Using @Persist with entities

If you wish to persist an entity in the session, you may use the "entity" persistence strategy:

...

This persistence strategy works with any Hibernate entity that is associated with a valid Hibernate Session by persisting only the id of the entity. Notice that no onPassivate() method is needed; when the page renders the entity is loaded by the id stored in the session.

Using @SessionState with entities


Since
since5.2


The default strategy for persisting Session State Objects is "session". Storing a Hibernate entity into a <HttpSession> is problematic because the stored entity is detached from the Hibernate session. Similar to @Persist("entity") you may use the "entity" persistence strategy to persist Hibernate entities as SSOs:

...


For this purpose you need to set the value of the symbol <HibernateSymbols.ENTITY_SESSION_STATE_PERSISTENCE_STRATEGY_ENABLED> to <true>:

...

Alternatively you can apply the "entity" persistence strategy to a single Hibernate entity:

...

Committing Changes

All Hibernate operations occur in a transaction, but that transaction is aborted at the end of each request; thus any changes you make will be lost unless the transaction is committed.

The correct way to commit the transaction is via the @CommitAfter annotation:

...

In this example, the Person object may be updated by a form; the form's success event handler method, onSuccess() has the @CommitAfter annotation.

...

First definine your DAO's service interface:

...

Next, define your service in your application's Module class:

...

Finally, you should use the HibernateTransactionAdvisor to add transaction advice:

...

This advice method is configured to match against any service whose id ends with "DAO", such as "PersonDAO".

...