What is JDBC?
JDBC stands for Java Database Connectivity and provides a set of Java API for accessing the relational databases from Java program. These Java APIs enables Java programs to execute SQL statements and interact with any SQL compliant database.JDBC provides a flexible architecture to write a database independent application that can run on different platforms and interact with different DBMS without any modification.
Pros and Cons of JDBC
Pros of JDBC | Cons of JDBC |
---|---|
|
|
Why Object Relational Mapping (ORM)?
When we work with an object-oriented systems, there's a mismatch between the object model and the relational database. RDBMSs represent data in a tabular format whereas object-oriented languages, such as Java or C# represent it as an interconnected graph of objects. Consider the following Java Class with proper constructors and associated public function:public class Employee { private int id; private String first_name; private String last_name; private int salary; public Employee() {} public Employee(String fname, String lname, int salary) { this.first_name = fname; this.last_name = lname; this.salary = salary; } public int getId() { return id; } public String getFirstName() { return first_name; } public String getLastName() { return last_name; } public int getSalary() { return salary; } }Consider above objects need to be stored and retrieved into the following RDBMS table:
create table EMPLOYEE ( id INT NOT NULL auto_increment, first_name VARCHAR(20) default NULL, last_name VARCHAR(20) default NULL, salary INT default NULL, PRIMARY KEY (id) );First problem, what if we need to modify the design of our database after having developed few pages or our application? Second, Loading and storing objects in a relational database exposes us to the following five mismatch problems.
Mismatch | Description |
---|---|
Granularity | Sometimes you will have an object model which has more classes than the number of corresponding tables in the database. |
Inheritance | RDBMSs do not define anything similar to Inheritance which is a natural paradigm in object-oriented programming languages. |
Identity | A RDBMS defines exactly one notion of 'sameness': the primary key. Java, however, defines both object identity (a==b) and object equality (a.equals(b)). |
Associations | Object-oriented languages represent associations using object references where as am RDBMS represents an association as a foreign key column. |
Navigation | The ways you access objects in Java and in a RDBMS are fundamentally different. |
What is ORM?
ORM stands for Object-Relational Mapping (ORM) is a programming technique for converting data between relational databases and object oriented programming languages such as Java, C# etc. An ORM system has following advantages over plain JDBCS.N. | Advantages |
---|---|
1 | Lets business code access objects rather than DB tables. |
2 | Hides details of SQL queries from OO logic. |
3 | Based on JDBC 'under the hood' |
4 | No need to deal with the database implementation. |
5 | Entities based on business concepts rather than database structure. |
6 | Transaction management and automatic key generation. |
7 | Fast development of application. |
S.N. | Solutions |
---|---|
1 | An API to perform basic CRUD operations on objects of persistent classes. |
2 | A language or API to specify queries that refer to classes and properties of classes. |
3 | A configurable facility for specifying mapping metadata. |
4 | A technique to interact with transactional objects to perform dirty checking, lazy association fetching, and other optimization functions. |
Java ORM Frameworks:
There are several persistent frameworks and ORM options in Java. A persistent framework is an ORM service that stores and retrieves objects into a relational database.- Enterprise JavaBeans Entity Beans
- Java Data Objects
- Castor
- TopLink
- Spring DAO
- Hibernate
- And many more
Persistence
Hibernate
ORM is concerned with helping your application to achieve persistence.
So what is persistence? Persistence simply means that we would like our
application’s data to outlive the applications process. In Java terms,
we would like the state of (some of) our objects to live beyond the
scope of the JVM so that the same state is available later.
Relational Databases
Specifically, Hibernate ORM is concerned with data persistence as it applies to relational databases (RDBMS). In the world of Object-Oriented applications, there is often a discussion about using an object database
(ODBMS) as opposed to a RDBMS. We are not going to explore that
discussion here. Suffice it to say that RDBMS remain a very popular
persistence mechanism and will so for the foreseeable future.
The Object-Relational Impedance Mismatch
'Object-Relational
Impedance Mismatch' (sometimes called the 'paradigm mismatch') is just a
fancy way of saying that object models and relational models do not
work very well together. RDBMSs represent data in a tabular format (a
spreadsheet is a good visualization for those not familiar with RDBMSs),
whereas object-oriented languages, such as Java, represent it as an
interconnected graph of objects. Loading and storing graphs of objects
using a tabular relational database exposes us to 5 mismatch problems…
Granularity
Sometimes
you will have an object model which has more classes than the number of
corresponding tables in the database (we says the object model is more
granular than the relational model). Take for example the notion of an
Address…
Subtypes (inheritance)
Inheritance
is a natural paradigm in object-oriented programming languages.
However, RDBMSs do not define anything similar on the whole (yes some
databases do have subtype support but it is completely
non-standardized)…
Identity
A RDBMS defines exactly one notion of 'sameness': the primary key. Java, however, defines both object identity
a==b
and object equality a.equals(b)
.Associations
Associations
are represented as unidirectional references in Object Oriented
languages whereas RDBMSs use the notion of foreign keys. If you need
bidirectional relationships in Java, you must define the association
twice.
Likewise, you cannot determine the multiplicity of a relationship by looking at the object domain model.
Data navigation
The
way you access data in Java is fundamentally different than the way you
do it in a relational database. In Java, you navigate from one
association to an other walking the object network.
This
is not an efficient way of retrieving data from a relational database.
You typically want to minimize the number of SQL queries and thus load
several entities via JOINs and select the targeted entities before you
start walking the object network.
Thank you for sharing this blog. This blog will help to improve my knowledge.
ReplyDeleteSpring and Hibernate Training in Chennai | Spring and Hibernate Training | Hibernate course in Chennai | Hibernate Training institute in Chennai
thank you.
ReplyDeletepython3 tutorial
Hibernate tutorial