Lazy Fetching : fetching data on demand iska use @ManytoMany aur
@OnetoMany me krte h
lazy=true ---> means not to fetch child object
lazy=false ---> means load both parent and child object
fetch=FetchType.LAZY
Eager fetching : it means jb parent load hoga tb child b load hoga
iska use @ManytOone aur @OnetOone me krte h
jse coure class h or usme list h stundent class ki to jb course ko call karege tb
student b load hoga.
fetch=FetchType.EAGER
-----------------------------
difference between bidirectional and unidirectional in hibernate
Bidirection: B. means allow navigation from both side.
Class Person{
private String fName;
private String lName;
private int age;
private Address address;
//getters and setters;
Class Address{
private String doorNo;
private String street;
priavate String city;
private String country;
private Person person;
yha dono class ke pass dono class ki information h to ye bidirectional h
Unidireaction: U. means allow navigation from one side.
EX: public class Employee{
private String name;
private int age;
private String phone;
private Department dept;
//getters and setters
}
public class Department{
private String deptId;
private String deptName;
private deptLocation;
//getters and setters
}
yha employee ke pas Department class ki detail h but Department k pass Employee ki
detail nahi h to ye Unideirectional h.
---------------------------------------
annotation used in hibernate
A:-@Entity,@Table,@Column @id, @GenricGenerator ,@GeneratedValue,@NotNull,
@NotEmpty,@Email ,@onetomany, @manytomany, @manytoone,@onetoone
---------------------------------------
which interface you have used in your project
A: SessionFactory
Session
Transaction
Criteria
---------------------------------------
What is Hibernate?
A.hib is an ORM tool that allow you to map pojo to relational database tables
using xml or annotations.
-------------------
IS SESSIONFACTORY A THREAD-SAFE OBJECT?
A. YES, SESSIONFACTORY IS A THREAD-SAFE OBJECT,MANY THREADS CAN NOT ACCES IT
SIMUNTANESOULY.
it creates one object per application.
----------------------------------------
IS SESSION A THREAD-SAFE OBJECT?
A. NO, SESSION IS NOT THREAD-SAfE OBJECT, MANY THREADS CAN ACCESS IT
SIMUNTANESOULY.
it creates one object per request/client.
---------------------
WHAT IS SESSION ?
A. IT IS A FACTORY OF TRANSACTION AND IT HOLDS FIRST LEVEL CACHE. SESSION IS USED
TO GET PHYSICAL CONNECTION BETWEEN DATABASE AND APPLICATION.
----------------------------
WHAT IS SESSIONFACTORY ?
A. IT IS A FACTORY OF SESSION AND IT HOLDS SECOND LEVEL CACHE.SESSIONFACTORY IS
HEAVYWEIGHT OBJECT SO USUALLY IT IS CREATED DURING APPLICATION STARTUP
AND KEPT FOR LATER USE.
------------------------
hibernate vs jdbc
A.hib is db independent
jdbc is db depependent
in hib no need to creat table
in jdbc need to creat table
IN HIB NO NEED TO CHANGE QUERY IF NEED TO CAHNGE DATABASE IN APPLICATION IN
FUTURE.
IN JDBC NEED TO CHANGE QUERY IF NEED TO CAHNGE DATABASE IN APPLICATION IN FUTURE.
----------------------------
dialact:The dialect specifies the type of database used in hibernate. // konsa
database use kr rhe h vo btata h.
d. is an interpreter which convert hql query to native query.
it depends on which dialect you are using.
it converts object to sql and sql to objcet.
---------------------------------------------------
criteria:it is an interface for fetching entities by criteria object. it used for
search operation.
EX : Criteria cr = session.createCriteria(Employee.class);
cr.add(Restrictions.eq("salary", 2000));
List results = cr.list();
cr.add(Restrictions.gt("salary", 2000));
// To get records having salary less than 2000
cr.add(Restrictions.lt("salary", 2000));
// To get records having fistName starting with zara
cr.add(Restrictions.like("firstName", "zara%"));
// Case sensitive form of the above restriction.
cr.add(Restrictions.ilike("firstName", "zara%"));
// To get records having salary in between 1000 and 2000
cr.add(Restrictions.between("salary", 1000, 2000));
// To check if the given property is null
cr.add(Restrictions.isNull("salary"));
// To check if the given property is not null
cr.add(Restrictions.isNotNull("salary"));
// To check if the given property is empty
cr.add(Restrictions.isEmpty("salary"));
// To check if the given property is not empty
cr.add(Restrictions.isNotEmpty("salary"));
----------------------------------
HQL : IT IS A HIBERNATE QUERY LANGUAGE.
EX : String hql = "FROM Employee";
Query query = session.createQuery(hql);
List results = query.list();
---------------------------------------
criteria Hql:
c. is suitalbe for dynamic queries. hql is suitable for static queries
only for select operation. for all crud operation.
take more time. take less time.
-----------------------------------------
save vs persist:
1)both are used for save the data.
2)save ka return type serializable h and persist ka void h.
3)save is only supported by Hibernate, persist is also supported by JPA
--------------------------------
get() vs load():
1)get() null return krta h if obj. is not found and load() objNotFound exception
throws krta h.
2)get() always hit database and load() doesnt hit the database.
3)get() return real object and load() return proxy object
4)database se data fetch krta h and load() session se data fetch krta h.
----------------------------
update() vs merge():
1)update means edit something and merge means combine something.
2)update() me session open h tabhi update hoga and merge() me session close hone k
bad bhi update kr skte h.
3)update() ka return type void h and merge() ka return type object h.
---------------------------
transient: obj t. state me tab hota h jab vo newly creat hua h but koi primary key
na ho aur session se assosiate na ho.
persisent: obj p. state me tab hota h jab session se assosiate ho aur databse me
data save kiya ho.(save(),saveOrUpdate() )
detached: obj d. state me tab hota h jab session se assosiate na ho aur session
close ho gya ho.(evict(),clear(),close())
-----------------------------------------
oneToMany: @oneToMany annotatin use krte h and @JoinColumn,@orderColumn use krte h.
Ex: there can be many answers for a question and each answer may have its own
information that is why we have used
list in the persistent class (containing the reference of Answer class) to
represent a collection of answers.
manyToMany: @manyToMany Annotation use krte h and @JoinTable use krte h.three
tables will be created.
oneToone: @oneToone annotation use krte h and @primaryKeyJoinColumn use krte h.
Ex:one employee can have one address and one address belongs to one employee only.
Here, we are using bidirectional association.
ManyToOne: @ManyToone annotation use krte h and @primaryKeyJoinColumn use krte h
Ex:In this example, every employee has one company address only and one address
belongs to many employees.
---------------------------------------------
CACHE : CACHE IS USED TO IMPROVE THE PERFORMANCE OF THE APPLICATION.
3 TYPES:
1) FIRST LEVEL CACHE : ASSOSIATED WITH THE SESSION, BY DEFAULT ENABLE, WE CANNOT
DISABLE IT,IT'S MANDATORY.
2) SECOND LEVEL CACHE : ASSOSIATED WITH SESSIONFACTORY, BY DEFAULT DISABLE , WANT
TO USE THEN NEED TO CHANGE IN CFG.XML FILE.
in xml file :- <property
name="hibernate.cache.use_second_level_cache">true</property>
3) QUERY CACHE
<property name="hibernate.cache.use_query_cache">true</property>
--------------------------------------------
HOW TO CONFIGURE TRANSACTION IN HIBERNATE ?
Ans:- <bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- enables annotation based transaction -->
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
---------------------------------
<!--Data Connection Pool and Trasnsaction handling start -->
<bean id="dataSourceC3P0" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/demo_ors0" />
<property name="user" value="root" />
<property name="password" value="root" />
<property name="acquireIncrement" value="2" />
<property name="minPoolSize" value="1" />
<property name="maxPoolSize" value="5" />
<property name="maxIdleTime" value="60" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSourceC3P0" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.raystech.proj0.dto.UserDTO</value>
<value>com.raystech.proj0.dto.StudentDTO</value>
<value>com.raystech.proj0.dto.CollegeDTO</value>
<value>com.raystech.proj0.dto.MarksheetDTO</value>
<value>com.raystech.proj0.dto.RoleDTO</value>
<value>com.raystech.proj0.dto.MarksheetDTO</value>
<value>com.raystech.proj0.dto.SubjectDTO</value>
<value>com.raystech.proj0.dto.CourseDTO</value>
<value>com.raystech.proj0.dto.FacultyDTO</value>
</list>
</property>
</bean>
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- enables annotation based transaction -->
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
</beans>