Here is a boilerplate code to start playing with ToPIA, you will find it in the sample project.
As you can see, being ready to work with ToPIA is pretty straightforward.
DAO usage
Create entities (a.k.a. the C in CRUD)
As you can see, for each entities, you are provided with an interface (Stuff), and its implementation (StuffImpl) as a plain old Java Bean.
You can always test whether an entity is persisted or not.
You now just have to call
and the database will have, in its ‘author’ table, a row with a technical id.
Let’s see some other ways to create Book entities:
Another way to create an entity, in one line:
… or given a map of the properties
Retrieve entites (a.k.a. the R in CRUD)
All simple operations are available on the DAO.
Most of the time, we need to find a single entity among the whole database:
If you are not sure that this particular book is available, you may use.
If you’re not familiar with Optionals, it’s just convenient way to represent the possibility of an absent result: it’s a compile-safe, type-safe way to handle null values. If you prefer getting null, you may use findXXXOrNull instead of tryFindXXX.
If you want the first one among multiple results, you should use:
Every methods of this API starting by “try” or ending by “OrNull” handle when no results are returned. Otherwise, an exception will be raised.
Now, let’s do a bit of filtering.
This example should be self-explanatory: among the books authored by platon and being titled “La République” or “Le banquet”, we want the first one of the results sorted by the ISBN order. It’s a tryFind… so if there is no result at all, we will just get an Optional.absent().