Object Relational Mapping (ORM)
Object-relational mapping (ORM) is an application (library) to map your objects to a relational database; typically, you describe which classes and properties in the code map to which tables and columns in the database, and then the library does all the copying and translating. In this scenario, ORM sits between JDBC and the rest of your Java application.
Sql2o
Sql2o is a small java library, with the purpose of making database interaction easy. To use Sql2o, you must add it as a dependency to your project. Open gradle.build
and add the following line to the dependencies
block.
Sql2oAuthorDao
Let's provide an implementation for AuthorDao
interface that uses Sql2o
:
When we use a Sql2o object, a convenient way to open a Connection
is as follows:
In case this is the first time you see a try-with-resources statement, this construct ensures that the resource is closed at the end of the statement.1
Sql2oAuthorDao.add
The power of Sql2o is in binding to an object; let's see that in action:
Note that we return the generated id
integer value by the database from the add
method for confirmation.
info
For binding to work seamlessly you need to have the same name for fields in the object and column names in your table.2
Sql2oBookDao.listAll
With Sql2o, when fetching data from the database, the ResultSet
will automatically be filled into your POJO objects.
info
Similarly, you can define a Sql2oBookDao
class.
Important note on the foreign key constraint
When implementing BookDao.Add
to insert a new record into the Books
table using Sql2oBookDao.add
, you need a valid authorId
, since there is a foreign key constraint on that attribute. This means any value you use there must already exist in Authors
table; an author
with that particular id
value must be present in the Authors
table before trying to add the book.3
- A resource is an object that must be closed after the program is finished with it. ↩
- If fields' name are not the same as column names, you may use column mapping feature of Sql2o.↩
- We will revisit this issue in more details when we get into implementing MyBooksApp as a wb application. For now, make sure when adding a new book,
authorId
is valid.↩