3

I am a newbie to java and was reading about Object Relational Mapping. I found a term Object/Relational mismatch on this link Hibernate

Can anyone explain what is Object/Relational mismatch in terms of Java. I also read about it from haacked.com but could not get it properly.Explanation with example would be appreciable.

Naved Ali
  • 616
  • 1
  • 14
  • 31
  • Possible duplicate of [What is an Object-Relational Mapping Framework?](http://stackoverflow.com/questions/1152299/what-is-an-object-relational-mapping-framework) – Gaurava Agarwal Jul 11 '16 at 11:29
  • No its not a duplicate as in that question no one have discussed about Object Relational Mismatch. I read object relational mapping from there itself but I had confusion in object relational mismatch which no one have discussed there. – Naved Ali Jul 12 '16 at 07:34

2 Answers2

7

Hibernate is an ORM (Object-Relational Mapping) tool. Its primary purpose is to translate concepts from object-oriented programming, such as classes, inheritance and fields, to concepts used in relational databases, such as tables, rows and columns.

For example, a class corresponds to a database table, an object (instance of a class) corresponds to a database row, and a field corresponds to a database column.

The term "object/relational mismatch" refers to the fact that there is not a clear way to translate all the concepts from object-oriented programming to relational database concepts and vice versa. Hibernate attempts to solve this problem.

For example, how do you translate inheritance to relational database concepts? There's no such thing as inheritance in a relational database, so some way has to be invented to represent this in the database. Hibernate has different ways to do this, for example by having one table for the class hierarchy with a discriminator column to determine to which subclass a row maps, or by having a table per subclass.

Likewise, there are concepts that exist in a relational database that cannot easily be translated to object oriented programming concepts.

Nayuki
  • 17,911
  • 6
  • 53
  • 80
Jesper
  • 202,709
  • 46
  • 318
  • 350
  • This isn't strictly true. Tables and classes do not map 1:1 in the way you imply. Object composition says that a single object can require JOINs of several tables to express 1:m and m:n relationships. Relations are set-based; objects are not. – duffymo Jul 14 '16 at 14:38
1

ORM solutions try to make it possible for object-oriented programmers to forget that they're using relational databases and deal only in objects.

Start with the difference between object-oriented languages and SQL. OO languages are procedural; SQL is declarative.

Objects are instances of classes that encapsulate state and behavior together into a single software component. Relational databases express relationships between entities that follow set theory.

Objects can use inheritance and composition. Tables can be JOINed together.

I didn't read the Hibernate article, but these would be three of the main differences that I can think of. See if those are written in a way that resonates with you.

duffymo
  • 305,152
  • 44
  • 369
  • 561