+1

Object-Relational Mapping (Ruby Active Record)

I.What is Object-relational mapping ?

ORM1.jpg

  • Object/Relational Mapping (ORM) in computer science is a programming technique for converting data between incompatible type systems in object-oriented programming languages.

  • ORM creates a "virtual object database" that can be used from within the programming language.

  • There are both free and commercial packages available that perform object-relational mapping, although some programmers opt to create their own ORM tools.

  • ORM it's a completely ordinary library written in your language that encapsulates the code needed to manipulate the data, so you will NOT use Structured Query Language (SQL) anymore, but directly use an object of your language.

  • The Object part is the one you use with your programming language ( ruby object in this case )

  • The Relational part is a Relational Database Manager System ( A database that is ) there are other types of databases but the most popular is relational ( you know tables, columns, pk fk etc Oracle MySQL, MS-SQL, Postgress )

  • And finally the Mapping part is where you do a bridge between your objects and your tables

  • Such as :

    • Java: Hibernate ORM
    • Ruby: Active Record
    • PHP : Propel or Doctrine. (I prefer the last one).
    • Python : the Django ORM or SQLAlchemy (The last one is my favorite ORM ever).

DEMO

  • JDBC in Java
List<Book> books = new List(<Book>);
String sql = "SELECT book FROM library WHERE author = 'Linus'";

//STEP 1: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");

//STEP 2: Open a connection
Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);

//STEP 3: Execute a query
Statement stmt = conn.createStatement();
data = stmt.executeQuery(sql);

//STEP 5: Extract data from result set
while (row = data.next()){
   book = new Book();
   book.setAuthor(row.get('author');
   books.add(book);
}
  • With an ORM, such as Ruby Active Record it would look like that :

books = Book.all

** What is your ideal about 10 line of code with 1 line of code ???**

II ORM TOOLS

A: ORM Ruby Active Record

1. What is Ruby Active Record?

  • ORM tool.
  • This is a toolfor communicate from Ruby Project with the DB.
  • Active Record is the M in MVC - the model - which is the layer responsible for representing data.
  • Active Record connects classes to relational database tables.
  • It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping (ORM).
  • Class - Table, Object - Record.

2. Active Record as an ORM Framework

mso20070601051.gif

  • Active Record gives us several mechanisms, the most important being the ability to:

    • Represent models and their data.
    • Represent associations between these models.
    • Represent inheritance hierarchies through related models.
    • Validate models before they get persisted to the database.
    • Perform database operations in an object-oriented fashion.

3. Use Active Record.

  • Install gem Active Record
$ gem install activerecord
  • Make sure you already have a DB name "my_db" in your DBMS, and it have a table name "companies"
  • Let's write a Ruby program and name it as company.rb
  • ###Company class be mapped with companies table
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
  adapter: "mysql",
  username: "my_username"
  password: "my_password"
  host: "localhost",
  database: "my_db"
)

class Company < ActiveRecord::Base
# Company class be mapped with companies table
end

Company.create(name: "A")
Company.create(name: "B")
Company.create(name: "C")

Company.first

NOTICE: Naming between Class - Table

Screenshot from 2015-07-10 11:31:09.png

B: ORM Hibernate Java

1. Hibernate Java (ORM tool)

  • It is ORM (Object Relational Mapping) tool.
  • Hibernate is a framework for the development of java application to interact with the database.
  • Hibernate is an open source.
  • It simplifies, easily the way Java dev interact with the database.
  • Hibernate ORM tool internally uses the JDBC API to interact with the database.

orm.jpg

2 Hibernate Architecture

  • There are 4 layers in hibernate architecture
    • Java application layer
    • Hibernate framework layer
    • Backhand api layer and
    • Database layer

Let's see the diagram of hibernate architecture

arc2.jpg

  • Hibernate framework uses many objects session factory, session, transaction etc.
  • Along with existing Java API such as
    • JDBC (Java Database Connectivity)
    • JTA (Java Transaction API)
    • JNDI (Java Naming Directory Interface).

architecture.jpg

**3 Demo Hibernate **

C: Java ActiveJDBC Fast ORM for agile development

1. ActiveJDBC Fast ORM for agile development

Thanks.

  • My first time studying about this technical. please give me some comments or feedbacks so that i could be better myself.

(thankyou)


All Rights Reserved

Viblo
Let's register a Viblo Account to get more interesting posts.