Skip to main content

MappingsConceptIntroduction


àHibernate-Mappings:
àWhenever we want to save an Entity obj we need it corresponding table in the DB. That means one class obj will related to one table. But it is always possible bcz in real world business applications every obj will not be in an isolated state and they will be related with another obj’s for business requirements. So obj’s never be in an isolated state.
àWe can relate one obj with another obj in 2-ways
1. Using Inheritance
a) Generalization
b) Realization
2. Using Association
a) Association
b) Aggregation
c) Composition
àAssociation means one obj is linked to another obj but there will not be any owner of that relationship that means the life time of the parent and child are independent with each other. That means parent is deleted then child will not be removed and there is no owner b/w them.
àAggregation means one obj is linked to another obj and there will be an owner of that relationship. But here also life time of the parent and child are independent with each other. That means if owner is deleted then child will not be removed.
à Composition means one obj is linked or contains inside another obj and there will be an owner of that relationship. But here also life time of the parent and child are dependent with each other. That means if owner is deleted then child also will be removed.
àLet us take we have a class Teacher, Principle then we wanted to store Teacher info and Principle info where Principle is also a Teacher.
àIf at all we wanted to store a Teacher class obj then we need to create a table for that class and relating attributes to columns in DB. And if we want to store the Principle info then we need to store Principle info which contains attributes of Teacher class also, so in order to store Principle info we should re-declare the attributes of Teacher class in Principle due to the duplication will happen hence we need to extend form Teacher. But in order to store the Principle info we are using obj relation as Inheritance but there is no Inheritance in the Relational DB s/m’s to Inherit columns of Teacher into the Principle table hence we are unable to store the data in the DB-table bcz all attributes of the class are not physically available in the Principle class and there is no Inheritance concept in the DB. So this is 1st problem when we are storing the relational obj data into the corresponding table.
That means as we cannot impose directly as obj relation to its table relation in DB i.e Inheritance is there within the obj’s but Inheritance is not there with DB tables. That means the way we relating the obj’s is different from the way we relating the data in Relational model is different, the tables will expresses the data in an rows and columns using PK and FK keys and the obj’s will express the relation in terms of hierarchical manner using Inheritance or Association. So how we can be persist the data in to the tables using obj’ relations. This is the problem we are facing while working with ORM-Tech and this problem is called as "Impedance mismatch" problem.
Similarly there are multiple problems while working with ORM-Tech and all these are called as "Impedance mismatch" problems. There are 5-problems and they are
1. Granularity
2. Subtypes (Inheritance)
3. Navigation
4. Identity
5. Association
àImpedance mismatch:
àImpedance mismatch means the way we modelling the obj’s using object oriented prog language and the way we are trying to store the data in an Relational Model will be not be in sink. That means there is mis-match b/w the obj relation model to Relational DB model is called as "Impedance mismatch".
àTo avoid the above Impedance mismatch problems ORM-Tech given a solution called as Mappings-Concept.
1. Granularity mismatch:
The no.of obj’s that we have and the no.of underlying tables we have to persist is always may not be same. So in such a way how can we persist and how can retrieve the data this is the one problem.
Storing more classes ob’sàin less tables.
2. Subtypes:
How we can we persist data if one class is inheriting from another class.
3. Navigation:

Ex:
class Address {
  private String aadrLine1;
  private String aadrLine1;
  private String city;
  private String state;
}
class Person {
  private int personId;
  private String name;
}
àIf we store Person and Address in 2-different tables then how can we get the Person and Address when we are fetch the data in obj modelling bcz classes will not express the relationships directly bcz one person will have many Addresses and many Addresses belongs to one Person and many persons can have many Addresses that means by looking at the classes we cannot easily determine the relationships as one-one or one-many or many-many that means we navigate (trace) the obj relation directly by looking at the classes to build the join conditions while fetching the data.
àperson.address.city.equals(hyd);
This is the obj model but how the join can created to get the data we cannot easily determine using obj relation to the DB-model.
That means navigation talks about Finding the relation is difficult while fetching or retrieving the data.
4. Identity:
àEvery java obj will have 2-identities equals() and == to identify whether both obj’s are same or not.
àSimilarly in Relational DB we have PK to identify the row uniquely.
à
Person person1=new Person();
person1.setPersonId(1);
person1.setName("DB");
session.save(person1);
session.commit();

Person person2=new Person();
person2.setPersonId(1);
person2.setName("DB");
session.save(person2);
session.commit();

àIn java we can create multiple obj’s but it should not permit to create multiple obj’s with the same id to match the relational identity. But it is not so simple to maintain the obj identity an relational identity to be same bcz java obj’s can create multiple obj’s for a same class. That means there is problem how we can restrict or ensure to create an obj per one row of a Relational model DB table so that object identity and DB identity will be in sink or will be match.

àThat means we should not allow to create multiple obj’s with same identity. That means this problem is already solved by using the PK while working with the Cacheing. That means cache will not allow to create multiple obj’s to create if the record is already associated with the session.
5. Association:
àHow does the data can be persisted in the table if obj is even related to with Association that means the obj which contains another obj has to be persisted in DB by translating those obj as relational model. So we cannot identify the relation easily to persist the data in to the Relational DB.
Ex:
class Person {
  private String personId;
  private String name;
  private Passenger passenger;
}



class Passenger {
  private String tktNo;
  private String noOfTickets;
}
àHere one person can have multiple tickets so we cannot tell by looking at the obj relation in which way the data has been persisted in the DB.
àNote that Navigation talks about identifying the relationships while retrieving the data and Association impedance mismatch will talks about identifying the relation while persisting the data.
Hence to avoid the above Impedance mismatch problems ORM-Tech given a solution called as Mappings-Concept, and by using this Mapping Concept we will try to resolve all the problems one by one.

Comments

Popular posts from this blog

Mockito interview Questions

1.       Question 1. What Is Mockito? Answer : Mockito allows creation of mock object for the purpose of Test Driven Development and Behavior Driven development. Unlike creating actual object, Mockito allows creation of fake object (external dependencies) which allows it to give consistent results to a given invocation. 2.       Question 2. Why Do We Need Mockito? What Are The Advantages? Answer : Mockito differentiates itself from the other testing framework by removing the expectation beforehand. So, by doing this, it reduces the coupling. Most of the testing framework works on the "expect-run-verify". Mockito allows it to make it "run-verify" framework. Mockito also provides annotation which allows to reduce the boilerplate code. 3.       Question 3. Can You Explain A Mockito Framework? Answer : In Mockito, you always check a particular class. The dependency in that class is injected using m...

JAVA Expert Interview Questions Answers 2017

Java Basics ::  Interview Questions and Answers Home  »  Interview Questions  »  Technical Interview  »  Java Basics  » Interview Questions 1.     What is the difference between a constructor and a method? A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. 2.     What is the purpose of garbage collection in Java, and when is it used? The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used. 3.  ...

Java Example Program to Convert List to Set

import java.util.ArrayList; import java.util.HashSet; import java.util.Set; public class ListToSet {  /**   * @author Amarjit Kumar   * @category interview questions   *   * Description: Convert List to set in java with example program   *   */  public static void main(String[] args) {   ArrayList<String> arrList= new ArrayList<>();   arrList.add("Java");   arrList.add("Java");   arrList.add("List to String");   arrList.add("Example Program");   Set<String> strSet = new HashSet<String>(arrList);   System.out.println(strSet);   System.out.println(arrList);  } } /*  * Java program to convert list to set. Convert ArrayList of string to HashSet  * in java example program How to convert List to Set in java Set<String> strSet  * = new HashSet<String>(arrList); HashSet having a constructor which will take  * list as an ar...