Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an Object-Relational Mapping Framework? [duplicate]

Tags:

orm

As the title says; what is a ORM framework and what is it useful for?

like image 852
RCIX Avatar asked Jul 20 '09 08:07

RCIX


People also ask

What is an ORM framework?

In a nutshell, an ORM framework is written in an object oriented language (like PHP, Java, C# etc…) and it is designed to virtually wrap around a relational database. If you look at the name (ORM), it basically translates into: mapping objects to relational tables.

What is object relational mapping used for?

Object-relational mapping (ORM) is a technique that creates a layer between the language and the database, helping programmers work with data without the OOP paradigm.

What is object relational mapping ORM in Django?

One of the most powerful features of Django is its Object-Relational Mapper (ORM), which enables you to interact with your database, like you would with SQL. In fact, Django's ORM is just a pythonical way to create SQL to query and manipulate your database and get results in a pythonic fashion.


2 Answers

A simple answer is that you wrap your tables or stored procedures in classes in your programming language, so that instead of writing SQL statements to interact with your database, you use methods and properties of objects.

In other words, instead of something like this:

String sql = "SELECT ... FROM persons WHERE id = 10" DbCommand cmd = new DbCommand(connection, sql); Result res = cmd.Execute(); String name = res[0]["FIRST_NAME"]; 

you do something like this:

Person p = repository.GetPerson(10); String name = p.FirstName; 

or similar code (lots of variations here.) Some frameworks also put a lot of the code in as static methods on the classes themselves, which means you could do something like this instead:

Person p = Person.Get(10); 

Some also implement complex query systems, so you could do this:

Person p = Person.Get(Person.Properties.Id == 10); 

The framework is what makes this code possible.

Now, benefits. First of all, you hide the SQL away from your logic code. This has the benefit of allowing you to more easily support more database engines. For instance, MS SQL Server and Oracle has different names on typical functions, and different ways to do calculations with dates, so a query to "get me all persons edited the last 24 hours" might entail different SQL syntax just for those two database engines. This difference can be put away from your logic code.

Additionally, you can focus on writing the logic, instead of getting all the SQL right. The code will typically be more readable as well, since it doesn't contain all the "plumbing" necessary to talk to the database.

like image 109
Lasse V. Karlsen Avatar answered Oct 14 '22 07:10

Lasse V. Karlsen


From wikipedia:

Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages. This creates, in effect, 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.

It's good for abstracting the datastore (flat file / SQL / whatever) out in order to provide an interface that can be used in your code. For example, (in rails) instead of constructing SQL to find the first user in a users table, we could do this:

User.first 

Which would return us an instance of our user model, with the attributes of the first user in the users table.

like image 39
Codebeef Avatar answered Oct 14 '22 05:10

Codebeef