1) What is Hibernate Framework?
Hibernate is an open-source and lightweight Object-relational mapping or ORM tool that is used to store, manipulate and retrieve data from the database.
2) Why Hibernate Framework?
A.Hibernate framework provides support for XML as well as JPA annotations, that makes our code implementation independent.
B.Hibernate provides a powerful query language (HQL) that is similar to SQL. However, HQL is fully object-oriented and understands concepts like inheritance, associations and collections.
C.Hibernate cache helps us in getting better performance.
D.Hibernate provided Dialect classes.
E.Hibernate is easy to integrate with other Java EE frameworks, it’s so popular that Spring Framework provides built-in support for integrating hibernate with Spring applications.
F.Getting pagination in hibernate is quite simple.
What are the core interfaces of Hibernate?
SessionFactory (org.hibernate.SessionFactory)
Session (org.hibernate.Session)
Transaction (org.hibernate.Transaction)
Configuration
Query
Criteria
What is SessionFactory?
SessionFactory provides the instance of Session. It is a factory of Session. It holds the data of second level cache that is not enabled by default.
Is SessionFactory a thread-safe object?
Yes, SessionFactory is a thread-safe object, many threads cannot access it simultaneously.
What is Session?
It maintains a connection between hibernate application and database.
It provides methods to store, update, delete or fetch data from the database such as persist(), update(), delete(), load(), get() etc.
It is a factory of Query, Criteria and Transaction i.e. it provides factory methods to return these instances.
Is Session a thread-safe object?
No, Session is not a thread-safe object, many threads can access it simultaneously. In other words, you can share it between threads.
What is the difference between session.save() and session.persist() method?
save() returns the identifier (Serializable) of the instance.
Syntax:-public Serializable save(Object o)
persist() return nothing because its return type is void.
Syntax:-public void persist(Object o)
What is the difference between get and load method?
Lazy loading in hibernate improves the performance. It loads the child objects on demand.
Since Hibernate 3, lazy loading is enabled by default, you don't need to do lazy="true". It means not to load the child objects when parent is loaded.
How many types of association mapping are possible in hibernate?
There can be 4 types of association mapping in hibernate.
How to make a immutable class in hibernate?
If you mark a class as mutable="false", class will be treated as an immutable class. By default, it is mutable="true".
How to log hibernate generated sql queries in log files?
We can set below property for hibernate configuration to log SQL queries.
However we should use it only in Development or Testing environment and turn it off in production environment.
What is Named SQL Query?
Hibernate provides Named Query that we can define at a central location and use them anywhere in the code. We can created named queries for both HQL and Native SQL.
Hibernate Named Queries can be defined in Hibernate mapping files or through the use of JPA annotations @NamedQuery and @NamedNativeQuery.
2) Why Hibernate Framework?
A.Hibernate framework provides support for XML as well as JPA annotations, that makes our code implementation independent.
B.Hibernate provides a powerful query language (HQL) that is similar to SQL. However, HQL is fully object-oriented and understands concepts like inheritance, associations and collections.
C.Hibernate cache helps us in getting better performance.
D.Hibernate provided Dialect classes.
E.Hibernate is easy to integrate with other Java EE frameworks, it’s so popular that Spring Framework provides built-in support for integrating hibernate with Spring applications.
F.Getting pagination in hibernate is quite simple.
What are the core interfaces of Hibernate?
SessionFactory (org.hibernate.SessionFactory)
Session (org.hibernate.Session)
Transaction (org.hibernate.Transaction)
Configuration
Query
Criteria
What is SessionFactory?
SessionFactory provides the instance of Session. It is a factory of Session. It holds the data of second level cache that is not enabled by default.
Is SessionFactory a thread-safe object?
Yes, SessionFactory is a thread-safe object, many threads cannot access it simultaneously.
What is Session?
It maintains a connection between hibernate application and database.
It provides methods to store, update, delete or fetch data from the database such as persist(), update(), delete(), load(), get() etc.
It is a factory of Query, Criteria and Transaction i.e. it provides factory methods to return these instances.
Is Session a thread-safe object?
No, Session is not a thread-safe object, many threads can access it simultaneously. In other words, you can share it between threads.
What is the difference between session.save() and session.persist() method?
save() returns the identifier (Serializable) of the instance.
Syntax:-public Serializable save(Object o)
persist() return nothing because its return type is void.
Syntax:-public void persist(Object o)
What is the difference between get and load method?
get()
|
load()
|
1.Returns null if object is not found. 2) get() method always hit the database. 3) It returns real object not proxy. 4) It should be used if you are not sure about the existence of instance. | 1.Throws ObjectNotFoundException if object is not . 2.load() method doesn't hit the database. 3.It returns proxy object. 4.It should be used if you are sure that instance exists. |
What is the difference between update and merge method?
update() method
|
merge() method
|
1) Update means to edit something. 2) Update() should be used if session doesn't contain an already persistent state with same id. It means update should be used inside the session only. After closing the session it will throw error. | 1.Merge means to combine something.
2.merge() should be used if you don't know the state of the session, means you want to make modification at any time.
|
What are the states of object in hibernate?
There are 3 states of object (instance) in hibernate.
Transient: The object is in transient state if it is just created but has no primary key (identifier) and not associated with session.
Transient instances may be made persistent by calling save(), persist() or saveOrUpdate(). Persistent instances may be made transient by calling delete().
Transient instances may be made persistent by calling save(), persist() or saveOrUpdate(). Persistent instances may be made transient by calling delete().
Persistent: The object is in persistent state if session is open, and you just saved the instance in the database or retrieved the instance from the database.Any instance returned by a get() or load() method is persistent.
Detached: The object is in detached state if session is closed.
Detached instances may be made persistent by calling update(), saveOrUpdate(), lock() or replicate(). The state of a transient or detached instance may also be made persistent as a new persistent instance by calling merge().
Detached instances may be made persistent by calling update(), saveOrUpdate(), lock() or replicate(). The state of a transient or detached instance may also be made persistent as a new persistent instance by calling merge().
What is the difference between first level cache and second level cache?
Ans. 1. First level cache is enabled by default whereas Second level cache needs to be enabled explicitly.
2. First level Cache came with Hibernate 1.0 whereas Second level cache came with Hibernate 3.0.
3. First level Cache is Session specific whereas Second level cache is shared by sessions that is why First level cache is considered local and second level cache is considered global.
What is lazy loading in hibernate?2. First level Cache came with Hibernate 1.0 whereas Second level cache came with Hibernate 3.0.
3. First level Cache is Session specific whereas Second level cache is shared by sessions that is why First level cache is considered local and second level cache is considered global.
Lazy loading in hibernate improves the performance. It loads the child objects on demand.
Since Hibernate 3, lazy loading is enabled by default, you don't need to do lazy="true". It means not to load the child objects when parent is loaded.
How many types of association mapping are possible in hibernate?
There can be 4 types of association mapping in hibernate.
- One to One
- One to Many
- Many to One
- Many to Many
What are the inheritance mapping strategies?
There are 3 ways of inheritance mapping in hibernate.
There are 3 ways of inheritance mapping in hibernate.
- Table per hierarchy
- Table per concrete class
- Table per subclass
What are the collection types in Hibernate?
There are five collection types in hibernate used for one-to-many relationship mappings.
There are five collection types in hibernate used for one-to-many relationship mappings.
- Bag
- Set
- List
- Array
- Map
Is it possible to perform collection mapping with One-to-One and Many-to-One?
No, collection mapping can only be performed with One-to-Many and Many-to-Many
How to implement Joins in Hibernate?
There are various ways to implement joins in hibernate.
No, collection mapping can only be performed with One-to-Many and Many-to-Many
How to implement Joins in Hibernate?
There are various ways to implement joins in hibernate.
- Using associations such as one-to-one, one-to-many etc.
- Using JOIN in the HQL query. There is another form “join fetch” to load associated data simultaneously, no lazy loading.
- We can fire native sql query and use join keyword.
How to make a immutable class in hibernate?
If you mark a class as mutable="false", class will be treated as an immutable class. By default, it is mutable="true".
How to log hibernate generated sql queries in log files?
We can set below property for hibernate configuration to log SQL queries.
<
property
name
=
"hibernate.show_sql"
>true</
property
>
However we should use it only in Development or Testing environment and turn it off in production environment.
What is Named SQL Query?
Hibernate provides Named Query that we can define at a central location and use them anywhere in the code. We can created named queries for both HQL and Native SQL.
Hibernate Named Queries can be defined in Hibernate mapping files or through the use of JPA annotations @NamedQuery and @NamedNativeQuery.
No comments:
Post a Comment