Apache Cayenne 4.1 Video Tutorial
Apache Cayenne 4.1 Video Tutorial


hi everyone welcome to our Apache Cayenne tutorial Apache Cayenne is an object-relational mapping
framework for Java in other words, it’s a tool that helps Java
developers effectively talk to databases Apache Cayenne has been around since 2001 it’s free, open-source and is distributed
under the Apache 2.0 license in this video tutorial, we’ll cover the
following topics number one connecting the two worlds: Java and the database number two querying the database using Cayenne API Apache Cayenne takes the database-first approach
to object-relational mapping it allows you to design your data model in
a single place – the database other layers, such as object-relational mapping
and Java objects, are synchronized automatically via CayenneModeler or a build script, so you
don’t have to repeat yourself now let’s see how the DB-first approach
is realized in Cayenne 4.1 by the way, you can find the code for this
project on Github – please see the link in the description so, let us create a new Maven project with
Cayenne inside in the pom.xml file, we need to add a Cayenne
dependency a MySQL driver and a simple logger, so that
we can view and read logs now let’s create a package for our application that’s where we’ll be experimenting with
Cayenne and writing our code. we start with a simple executable Java class
with the main() method inside we’ll be running it to see how Cayenne works
and what you can do with it now that the Maven project is ready, we need
to create and start a database we’re going to use a Docker container we start it import the SQL dump and that’s it now everything is ready, and we can start
working with the Cayenne model now let us use Cayenne Modeler to create our
model the modeler is a graphical user interface
that helps you edit your Cayenne model first, we need to configure the database connection we are going to connect to the database we
just started in Docker connecting to the database is pretty intuitive we just need to specify the JDBC Driver
class the database connection string and the login and password second, we need to manually upload the MySQL
driver that’s it this was probably the most difficult part,
which we only have to do once now let’s reverse-engineer the database what we do here is we import the database
schema to our Cayenne model we should specify the Java package for all
our future Java classes this is probably the only setting that we
should worry about in the modeler if you are not happy with the names of your
model objects that were imported from the database, you can edit them
in the modeler this is how we change the names in the Java
part in this case, some object names in our Java
model will differ from the ones in the database but we’re touching just the Java layer and
not the database layer so, that’s okay now let us save the model to where our
project is located the last action we take in the modeler is
generating the Java code that’s it the code has been generated with just a push
of a button as you can see, all the paths were set automatically and here you can see the generated classes now let’s take a look inside here is our Java code with all the fields,
helper methods, setters and getters we didn’t have to write anything by hand we just imported the model from the database now let’s move on to actually running the
application the first step is to create a Cayenne runtime ServerRuntime is a key object that contains the
entire model and all the runtime information it is quite easy to create we enter the Cayenne config file name and
the connection properties they will have pretty much the same values
that we used in the modeler because we are connecting to the same database now we’re all set and we can try changing something
in the database using Cayenne first, let’s try to insert an Artist record every action starts with a context a Cayenne context is a lightweight object
that is required for any database operation now let’s try to create an artist, Picasso and save it to the database as you will see, we won’t succeed the first
time but let’s run our code anyway we’re connecting to the database and now we get an error, which was
expected Cayenne has validated the data against its
model and found that the Name field is required
for the Artist object that’s why it is throwing an error now let us try to create an artist that does
have a name that’s it, the insert operation has been
successfully committed and the ID value of 1 has been generated
for the new record now, let us try to update some data again, we start with the context in order to make each method independent,
we always create a new context if you wish, you can reuse an existing context but it takes just a few seconds to create
a new one now let’s select an object by ID we’ve just seen the artist’s ID, so let’s
use it and now let’s try to update something for example, let’s set a Date of Birth for
the artist we only use a setter and then commit the changes no other actions are required now let’s run our code and see what we get it is starting connecting to the database great we selected an Artist record in the database and updated its Date of Birth let us also add some related data for this
artist more specifically, let’s add some paintings and while we do that, let’s also see how
entity linking works in Cayenne first, let’s create a new context second, let us select an artist, with whom
we’ll be linking the paintings let’s use a different method this time instead of selecting the artist by ID, we
are going to find the artist by name now let us create our first painting again, by using the newObject
method of the context object we set the title and the estimated price and now, let us link the painting with the
artist you can do this by using setArtist of the
painting object now let’s create another painting linked
with the artist the link works in both directions it doesn’t matter if you set an artist for
a painting or vice versa the result will be the same that’s it now let us run our code as you can see, we selected an artist and
then inserted two Painting records within one transaction now let’s play with the data SELECT query we’ve already selected an Artist record
by ID and by name this time, let’s try a more complex query since we’ve got a little more data to play
with so, why not look for some paintings again, we are going to use ObjectSelect it is the most popular, the most effective
and convenient way of selecting data first, we simply select all the paintings then, let’s add a new method to a painting in fact, you can add your own fields and methods
to model classes and your changes will remain even after you
re-generate the code it works because Cayenne will overwrite the
parent classes while your changes will be stored in the
child classes so, we have redefined the painting’s toString
method to have more informative logging output here you can see that our simple query went
through and returned these records why don’t we try a more complex query let’s start with the where condition to construct it, we just use the generated
static fields here we use EstimatedPrice field from the
Painting class now let’s add another condition, which will
be applied to the linked Artist object as a result, we will see the paintings
priced over a hundred million and painted by the artists born after
the year eighteen hundred when we run our code, we can see that only
one painting has been found and here is the query that was generated it has a join and two conditions as you can see, the API allows you to create
complex queries rather conveniently it is also safe because it won’t allow you
to use incorrect fields so, you can do refactoring with a peace of mind here we can make use of prefetch which will pull the Artist record while
selecting the paintings it will be more efficient because you will
get more data within the same query meanwhile, we can also add a sorting clause
to our request let’s also add the artist name to the painting’s
toString output it will let us see that we have actually pulled
the linked Artist record together with the painting now, let’s run our code again and see the
results okay, it’s starting and, again, we’ve got just one SELECT request but this time we have not only selected
the paintings, but also the linked artist this is it for the tutorial part you can learn more about Apache Cayenne on
the official website or on the GitHub page for news and updates, please subscribe to
Cayenne on Twitter thanks for your attention goodbye

1 thought on “Apache Cayenne 4.1 Video Tutorial”

  1. MrDaedraeda says:

    I never heard of Cayenne, but this is a really great tool! This is intuitive yet powerful!

    Thank you for not only writing that great documentation, but also making this video. It helped me a lot seeing a simple example created from scratch. Great Work!

Leave a Reply

Your email address will not be published. Required fields are marked *