Hibernate
Hibernate
Hibernate
@OnetoMany me krte h
lazy=true ---> means not to fetch child object
lazy=false ---> means load both parent and child object
fetch=FetchType.LAZY
Class Address{
private String doorNo;
private String street;
priavate String city;
private String country;
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
---------------------------------------------------
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));
----------------------------------
---------------------------------------
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())
-----------------------------------------
manyToMany: @manyToMany Annotation use krte h and @JoinTable use krte h.three
tables will be created.
---------------------------------------------
3 TYPES:
1) FIRST LEVEL CACHE : ASSOSIATED WITH THE SESSION, BY DEFAULT ENABLE, WE CANNOT
DISABLE IT,IT'S MANDATORY.
--------------------------------------------
HOW TO CONFIGURE TRANSACTION IN HIBERNATE ?
---------------------------------
<!--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>
</beans>