Tuesday 6 February 2018

Hibernate


Hibernate is a ORM Tool, which transfers the java objects into Relation Database in the form of Objects.
Hibernate is a Open Source and Light weight.



ORM Tool : Converting the data between RDBMS and OOP Language is know as ORM.
  • Hibernate 
  • iBATIS 
  • Java Data Objects (JDO) 
  • Java Object Oriented Querying (jOOQ) 
  • Java Persistence API (JPA) 
  • MyBatis
Advantages:
  • Simple API provides for storing and retrieving the data from database. 
  • Supports Association, Inheritance, collections & Cache Mechanism.
  • If Table is not exist in the DB it will automatically create wont throw any error.
  • Connection Pool Mechanism is very simple to implement. 
  • Not specific to any database, we can use any type of RDBMS queries to access the db.
  • Automatically generates the Primary Key.         
Dis-Advantages :             

  • Lot of API need to learn.
  • Debugging and performance tuning is difficult.
  • Slower than Pure JDBC because Hibernate will generate lot of SQL Queries @ Runtime.

Difference between JDBC & Hibernate : 

Hibernate :

  • Hibernate provides cache mechanism.
  • Implementing Connection Pool mechanism is very easy. 
  • Not Specific to any database Queries are DBMS Specific.
JDBC :

  • Not supported for Cache Mechanism
  • Need to write different code for Connection pool.

Composite Primary Key : 
If Table having more than one primary key we can call it as Composite Primary key.

Criteria :

Projections :

Types of Hibernate :

  1. IDENTITY 
  2. SEQUENCE
  3. CUSTOM GENERATOR
  4. AUTO
Optimistic locking :
Frequently accessing the data on a table, that time lock will happen on table. To optimize that we can use "Versioning" or "Time stamp" on a table. Which acts as a cache, when ever values changes it will fetch the latest data. 

Cache :

When Cache will apply on api's?
Where ever frequent changes wont happen then we can implement cache on that api.
Ex : Search the particular data in Case Module.

2 Types of Cache in Hibernate :
1st Level Cache : Session Level Cache. Default cache in hibernate.
2nd Level Cache : Session Factory Level Cache. Need to configure and customize.

How Cache works ?
@Cacheable annotation will take care about cached  data.
@Cacheable(Value = "temp") : Value will take about the data changes in cache.
EhCache.xml : Need to specify the cache details by using Cache value. Name and value should be same in both. Here we will mention the cache timing based on this time cache will update the data.

Difference between Load() and Get()?

Load () : It wont hit the database and retrieve the value. Because it will create one proxy object with the given identifier. If Object is not found then it will through "Object Not Found Exception "

Get () : It will always hit the database to retrieve the values.If Row is not found it will throws "Object Not Found Exception".

Difference between Eager and Lazy Load :

Eager Loading : 
  • Load all the relationships data from the database.
  • Bit Slow.
  • By Default it will fetch eager.

Lazy Loading : 
  • Doesn't Load the relationship unless explicitly asked via getters.
  • More Faster.

Difference between Merge and Update : 
Update() : 
update returns serializable object.
When ever data is there then update will happen based on id or some other attribute.

Merge():
Merge return type is void.
Its acts as a persist() means, If we want to save any modification at anytime without knowing the state of an object then we can go for persist().


Aggregations: 

1. One To One Mapping: 
Ex : One Student will have One Laptop
@Entity
public class Student
{
@Id
Private int sid;
private String sname;
@OneToOne
private Laptop laptop;
------
------
}

2. One To Many Mapping & Many To One Mapping 
Ex : One Student will have Many Laptops 
@Entity
public class Student
{
@Id

Private int sid;
private String sname;
@OneToMany(mapped By = "sid")
private List<Laptop> laptop;
-----
-----
}
Note : If we didnt specify the mapped By = "sid", it will create a new table like Laptop_Student.

3. Many To Many Mapping
Ex : One Student will have Many Laptops.
        Multiple Laptops belong to One Student.

@Entity
public class Student
{
@Id

Private int sid;
private String sname;
@ManyToMany(mapped By = "students")
private List<Laptop> laptops;
-----
-----
}




@Entity
public class Laptop
{
@Id

Private int lid;
private String lname;
@ManyToMany(mapped By = "laptops")
private List<Student> students;
-----
-----
}


Note : If we didnt specify the Mapped By ="names" then it will create 4 extra tables.

No comments:

Post a Comment

SpringBoot

SpringBoot SpringBoot Application :  Pros & Cons :  SpringBoot Application creation using spring.io :  SpringBoot Application Annotation...