|
Torque
Guides
Howto Guides
Development
|
|
Working With Peers
|
Peers are an Object Relation mapping tool, built on top of Village that gives
you access to a relational database via Java objects. Peers act on a somewhat
lower level than other O-R mapping tools like Castor or Osage. This means that
you have to do some coding by hand, but it allows for the most flexible
solution.
Peers use Torque's Database adaptor classes that make uniform connection
to a wide range of databases possible. If your database is not supported
you can read the Database Adapter docs on
how to create a new adaptor for your database.
NOTE: If you would like to use Peers outside of the Turbine Servlet
(this is very common), then you need to use the TurbineConfig object to
initialize Turbine's code with the right TurbineResources.properties
file before you execute your Peer code. Please see the
javadoc for the TurbineConfig object for more details.
|
|
|
Capabilities
|
 |
 |
 |
 |
> I'm looking for an O-R layer for the next version of our system;
> somebody recommended that I evaluate Peers. I've identified 7
> structural cases and a few other criteria that I'd like in an O-R tool.
> Can an experienced Peers user or developer tell me how well it handles
> these situations?
>
> O-R structures:
> 1) simple (1 class, 1 table)
You use a tool called Torque to generate Peer classes for you. There
are 4 classes for each table. Base<table-name>Peer, Base<table-name>,
<table-name>Peer and Base<table-name>
The Base* classes contains all the functionality and should not be
change. The other two classes are empty and this is where your
application business logic goes. If you regenerate with torque only the
Base* classes changes. This allows you to change the schema, but still
keep your existing code.
> 2) 1:n
Torque will generate methods for you to access the relevant objects.
For example Category and Item. Category.getItems() will give all the
items for a category or Item.getCategory() will give the associated
Category for an Item.
Torque also generates methods for joining all objects with less db hits
as to improve performance.
> 3) n:m
You can use the methods generated by Torque for this, but it would
probably be more efficient to use a Criteria object. Joins are very
easy to do and you'll find that you'll be able to do complex multi-table
joins without a problem.
> 4) self join 1:n (object trees)
Same as (3). I have very efficient code that loads a tree from a table
into a in memory tree representation with a single db hit. If you're
interested I can give it to you.
> 5) self join n:m (object maps)
Same as (3).
> 6) simple inheritance (S extends B, each maps to a table with a shared
> primary key)
I don't think there is any support for this at the moment, but it could
probably be done.
> 7) polymorphic inheritance (S and T extend B, the application works with
> a collection of B)
Same as (6)
> The only other real requirement I've got is good documentation.
As an added bonus Peer allows you to create objects from a standard SQL
query. This gives you the opportunity to do things by hand wherever you
might find Peers lacking (which isn't a lot :-)
The documentation is coming along nicely, but there is room for
improvement. We'll help you with Peer if you help out with docs :-)
|
 |
 |
 |
 |
|
|
|
Database Maps
|
Peers make use of a DatabaseMap class that holds internal data about the
relational schema. You will seldom, if ever, need to work with the
DatabaseMap class. It is used internally by Peers to discover information
about the database at runtime.
There is exactly one DatabaseMap for each relational database that you
connect to. You may wish to connect to more than one database in your
application. You should then have one DatabaseMap for each of the
databases.
DatabaseMaps are constructed by classes called MapBuilders. Turbine has
a default MapBuilder, called TurbineMapBuilder, that creates a
DatabaseMap for Turbine's internal database structure. Again you should
seldom work with MapBuilder classes. They are used internally by Peers.
Usually there is a MapBuilder for each table in your database.
MapBuilder classes for new schemas can be generated for you automatically.
|
|
|
Peer Classes
|
Everything in Peers resolve around Peer classes. A Peer class has a
one-to-one mapping to a Database table. You use each table's associated
Peer class to do operations on that table. Peer classes can be generated
for you automatically.
Peer classes have static methods only, so you would never create objects of
Peer classes. It is not necessary to have objects on this level because
of the one-to-one mapping with a table. Peer methods are thread safe.
|
|
|
Data Objects
|
A Data Object holds information about a single row of a specific table.
Data Objects can be generated automatically for you. It takes the form
of Bean properties for each field of the table.
Data Objects are used almost exclusively with their related Peer classes.
Where peer classes "wrap around" around a database table, a Data Object
"wrap around" individual rows of the table. The two always go together.
You normally use Data Objects in one of two ways. The most common way
is to extract data after you called a doSelect on a Peer class. The
doSelect method returns a vector of Data Objects that holds the data of
the resultset. Secondly you can create Data Objects and pass it to the
overloaded doInsert and doUpdate methods of the relevant Peer class.
|
|
|
Criteria Objects
|
Criteria is an abstraction of the criteria of an sql query. We use
criteria objects to specify the criteria of a sql statement. The
database adaptor classes contains information on how this Criteria object
will be translated to different flavours of sql.
Criteria is in effect a map of field names and values that forms the
criteria of a query. By default the comparison is equals (=) but you
can define any comparison operator (<,>,<=,>=,IN,etc.).
Criteria can also be used to do some other sql function like ORDER BY or
DISTINCT. If Criteria is too limited for your purposes (which should not
happen often) you are still free to use raw sql queries.
There is more information on the use of the Criteria class in a seperate
Criteria Howto.
|
|
|
Advanced Peer Techniques
|
In this section I'm going to try and explain a bit more about using
Peers than just run of the mill selects, inserts and updates.
However, this is by no means the be-all and end-all of Peer usage.
It is just some ideas that I have found to work well.
|
|
|
Subclassing
|
Usually when we begin to add extra code for Peer classes we end up with
the problem of where to add the code. If we add it to the generated
BaseItemPeer and BaseCategoryPeer classes we will lose everything if
(and trust me this does happen) we need to regenerate these classes.
The solution is to add the extensions to the subclasses ItemPeer and
CategoryPeer. We can regenerate the Peer classes at any time and still
maintain any changes we add. Regenerating the Peers from the XML schema
only affects the Base classes. The other classes are left untouched.
|
|
|