Blog Image

Active JDBC  

What is ActiveJDBC?            

ActiveJDBC is a lightweight ORM and it focuses on simplifying the interaction with databases by removing the extra layer of typical persistence managers and focuses on the usage of SQL rather than creating a new query language.Additionally, it provides its own way of writing unit tests for the database interaction through the DBSpec class.

Let?s see how this library differs from other popular Java ORMs and how to use it

1. It infers the DB schema parameters from a database so that we no need to mapping entities to underlying tables      

2.No session No DB component management and no need to learn a new query language

3.No need to write a model class in server side

4.This implementation provides us test database, once execution did it will auto clean the schema  

Let?s go with some Pre-requisite and environment setup to work with Active JDBC

Setting up the Library

First, we need to add an ActiveJDBC dependency


Add ActiveJDBC Instrumentation plugin in pom.xml


JDBC instrumentation we can directly invoke a static method like below We should use the latest version of ActiveJDBC i.e 2.0 version.

What is the use of instrument plugin?

Instrumentation is bytecode manipulation that happens after a compile phase. It adds static methods from superclass to a subclass

Here superclass is org.javalite.activejdbc.Model with which we are going to extends our Entity so that my Entity can achieve all features of JaVale provided Model

Assume to fetch the list of record based on the condition we need to write SQL like below

Select * from Table where field=?

Same way using Active

Assume my Entity class is Person

List<Person> retirees = Person.where ("age >=?", 65);

If you mark above syntax

Class Person Extends org.javalite.activejdbc.Model {}

So that using Person class we can call a predefined static method like where () or order () etc. as Person inherited from org.javalite.activejdbc.Model

So we need to add this instrumentation plugin, without instrumentation, ActiveJDBC would not be able to know which class is being called, and as a result, what table to query

 After adding instrument plugin in pom.XML we need to run one build

mvn process-classes

mvn activejdbc-instrumentation: instrument

You can run any of one build, after build you will see in the console that instrument start and end like below


Now Getting started with small POC where we can find how to make DB connection and how we can do CRUD operation

Development:

Before developing application first we should create the table manually as this ORM just abstraction above the JDBC implementation, wherein JDBC we don?t have table auto creation features

Create Table:


Create Model:

Let?s create Entity/Model class now


We can create a simple model with just one line of code ? it involves extending the Model class

In Person Model class we no need to declare any attribute as we already defined the attribute as a column while creating Table

Connecting to a Database:

ActiveJDBC provided 2 class to connect with databases

1.   Base

2.   DB

Base:

If we are using one database then we should go for a Base utility to create a connection like below


Once it opens the connection object is then attached to the current thread, and can be consumed by any ActiveJDBC API.

DB:

If we are using more than one database we should go for DB

Syntax to create a connection


CRUD Operation:

1. Insert record


This is self-explanatory. Models are somewhat like maps. There are no setters or getters. You can still write them if you want

Insert Record approach: 2

The same logic can be written in one line as below


Database:


2. Fetching a Record

Fetch single record based on the last name


This line will find an instance of Person (conditionally), if one exists, or null if one does not exist

Fetch records based on condition using inbuilt where clause


Fetch all records


3. Update Record

Assume I want to update person last name based on id

Update Person set last_name=? where id=?

Steps:

First I need to get that Person object based on ID

Then on same Person object need to set the last name and then update Person Object like below


4. Delete Record

Delete record based on field


Delete all records


Pagination

Fetch a list of record based on last_name and paginate it order by id


About author

User Image
Basant

Well, This is Basant Having Experience in Java/J2ee development platform , currently working as a technology consultant in one of MNC and am passionate about Java technology completely lost in code . Exploring, Learning and Sharing.

0

-Comments

Be the first person to write a comment for this Blog
Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.