Java Reference
In-Depth Information
When the application starts, Hibernate generates and executes SQL
commands to create the database schema. The tables automatically
contain columns that correspond to the properties of the entity classes
as well as extra columns to manage the one-to-many/many-to-one rela-
tionships. The data is moved between the database and the model
classes, in both directions, without requiring us to write a single line of
SQL. And that's not all—Stripersist allows us to use our entity classes
directly, taking care of reading objects from the database for us. Let's
talk about that in more detail.
Stripersist Type Conversion and Formatting
Stripersist registers entity classes and does type conversion for them on
the fly. The type converter finds the
@Id
property on each
@Entity
class
(or
@MappedSuperclass
) and uses it to convert a
String
to an entity class
by loading the corresponding object from the database. Stripersist also
creates formatters that do the opposite, which is to produce a
String
from the model object's
@Id
.
We discussed writing type converters to load model objects from ID
to do this ourselves; Stripersist's type converter does it automatically.
Here's how it works. Say we have a
Contact
class with an
@Id
property
1. A request comes in with a
someContact=5
parameter to be bound to
an action bean's
someContact
property of type
Contact
. Since
Con-
tact
has the
@Entity
annotation, Stripersist's
TypeConverter<Entity>
is invoked.
2. The type converter finds the
@Id
property on the
Contact
class and
determines its type,
ID
.
3. Stripersist asks Stripes for a type converter for
ID
and calls its
convert
( ) method with the
String
parameter
"5"
.
4. The type converter returns the converted
id
value of type
ID
. This
works automatically if Stripes has a built-in type converter for the
ID
type; otherwise, we need to provide our own.
5. The type converter then calls the JPA
EntityManager
's
find
( ) method
with the converted
id
object.
6. The
EntityManager
loads the
Contact
object from the database and
returns it.
Search WWH ::
Custom Search