Wednesday, February 3, 2021

Hibernate

Q. What is ORM ?
Ans: ORM stands for object/relational mapping. ORM is the automated persistence of objects in a Java application to the tables in a relational database.
Q. What is Hibernate?
Ans:Hibernate is a pure Java object-relational mapping (ORM) and persistence framework that allows you to map plain old Java objects to relational database tables.

Q.Why do you need ORM tools like hibernate?

The main advantage of ORM like hibernate is that it shields developers from messy SQL. Apart from this, ORM provides following benefits:

  • Improved productivity
    • High-level object-oriented API
    • Less Java code to write
    • No SQL to write
  • Improved performance
    • Sophisticated caching
    • Lazy loading
    • Eager loading
  • Improved maintainability
    • A lot less code to write
  • Improved portability
    • ORM framework generates database-specific SQL for you

Q. What are the differences between First level cache and second level caches?

Ans: There are following differences between First level cache and second level caches

First level cacheSecond level cache
It is associated with session objectSecond level cache is associated with session factory object
By default hibernate uses first level cache on pre-transaction basisSecond level cache will have to enable by developer when you need.
Hibernate uses this cache mainly to reduce the number of SQL queriesHibernate uses second level cache  mainly to reduce database traffic


16.What’s the difference between load() and get()? 

load() vs. get() :-

load() get() 
Only use the load() method if you are sure that the object exists. If you are not sure that the object exists, then use one of the get() methods. 
load() method will throw an exception if the unique id is not found in the database. get() method will return null if the unique id is not found in the database. 
load() just returns a proxy by default and database won’t be hit until the proxy is first invoked.  get() will hit the database immediately. 

Q. What is HQL?

Ans: Hibernate Query Language (HQL) is same as SQL (Structured Query Language) but it doesn't depends on the table of the database. Instead of table name, we use class name in HQL. So it is database independent query language.

Advantage of HQL

There are many advantages of HQL. They are as follows:

  • database independent
  • supports polymorphic queries
  • easy to learn for Java Programmer

Q. What is Criteria?

Ans: The Hibernate Criteria Query Language (HCQL) is used to fetch the records based on the specific criteria. The Criteria interface provides methods to apply criteria such as retreiving all the records of table whose salary is greater than 50000 etc.

Advantage of HCQL

The HCQL provides methods to add criteria, so it is easy for the java programmer to add criteria. The java programmer is able to add many criteria on a query.

Q. What is the difference between HQL and SQL?

Ans: HQL is database independent when you switch your application from one database to another database you do not need to change HQL queries. whereas SQL are database dependent and you need to change it when you change database. 

Q. What is Eager fatching?

Ans: It is just like outer join which return data of parent object as well as child object .

OR

It fetch all data from both table whether they are matched or not. 

It is enable by fetch=join. In case of Eager fatching one query is fired.

Q. What is Lazy Fatching?

Ans: Lazy fatching is done by on demand. It fatch data either of parent object or child object but on demand. It is enable by lazy=true. In case of lazy fatching one query is fired but if we demand for child object like getBids() method then second query will be fired.

Q. What is Batch Fatching?

Ans: Batch fetching get the data in form of batch or collection. by specifying list of primary key or foriegn key.

Q. Who convert HQL into SQL?

Ans: Dilect convert HQL into SQL

Q. Who convert crieteria into SQL?

Ans: Dilect convert criteria into SQL.

Q. Which Dilect did you use in your application?

Ans: We used Mysql dilect in our application.

Q. How did you make DCP in hibernate your application?

Ans: Session factory itself a DCP.

Q. Session factory is mutable or immutable?

Ans: Session factory is immutable

Q. What is transient, persistent and detached object?.

Ans: 

Transient: Till the time object is newly created and not stored in the database. It is called transient.


No comments:

Post a Comment

String coding questioon

  How do you reverse a given string in place? ( solution ) How do you print duplicate characters from a string? ( solution ) How do you chec...