0% found this document useful (0 votes)
25 views2 pages

Hibernate

Hibernate is an ORM tool that maps Java objects to database tables. It eliminates JDBC boilerplate code and supports HQL, transactions, and caching for better performance. Important interfaces include SessionFactory, Session, and Transaction. Common annotations map entities to tables and relationships. Mappings include one-to-one, many-to-one, and many-to-many. Configuration and mapping files define the setup. Steps include creating POJOs, mappings, configuration, and running code. Caching occurs at the session and second level using tools like EHCache.

Uploaded by

Akram Shuja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Hibernate

Hibernate is an ORM tool that maps Java objects to database tables. It eliminates JDBC boilerplate code and supports HQL, transactions, and caching for better performance. Important interfaces include SessionFactory, Session, and Transaction. Common annotations map entities to tables and relationships. Mappings include one-to-one, many-to-one, and many-to-many. Configuration and mapping files define the setup. Steps include creating POJOs, mappings, configuration, and running code. Caching occurs at the session and second level using tools like EHCache.

Uploaded by

Akram Shuja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

1. What is Hiberanate -> ORM tool used to map java objects and database tables.

Provides JPA implementation hence we can use JPA annotations as well as xml
configurations to achieve mapping.

2. Advantages - Eliminates JDBC biler plate. Supports HQL whic is more Object
oriented. Provides transaction management implicitly. Throws JDBC or Hibernate
Exception - unchecked - hence no need to handle. Also supports caching for better
performance.

3. Important Interfaces in Hibernate:


- SessionFactory -> Instance of this is used to retrieve Session Objects for db
operations. Initilaize once and cache to reuse again
- Session -> Factory for transaction. Used for connecting application with DB.
Used to get a physical connection. Provided method for CRUD operation
- Transaction -> Single unit of work
SessionFactory factory = metadata.getSessionFactoryBuilder().build();
Session session = factory.openSession();
Transaction t = session.beginTRansaction();
session.save(persistentObject); t.commit(); factory.close(); session.close();

4. Important Annotations
Entity, Table, Access(Field or porperty(getters/setters)), Id, EmbeddedId, Column,
GeneratedValue, OneToOne, Cascade, PrimaryKeyJoinColumn

5. Mappings - OnetoOne, ManytoOne, ManyToMany


- ManyToOne -> Student <-> Degree @ManyToMany(targetEntity=Degree.class,
cascade={CascadeType.All})
@JoinTable(name="DegreeSTudentMtoN", joinColumns= {@JoinColumn(name="StudentId")},
InverseJoinCOlumns={@JoinColumn(name="CertificateId")})

6. Hibernate Configuration File(hibernate.cfg.xml)


- <hibernate-configuration><sessionfactory>ddl.auto, dialect(Oracle9), url, user,
password, driverclass</hibConf></sessionFact)

7. Hibernate Mapping File. For mapping entity to tables - in the form of


class_name.hbm.xml

8. Steps -> Create POJO - Craete mapping file - Create config file - Creare
Repository CLass - Run

9. get() and load(). get loads the date as soon as it is called, load returns a
proxy object and loads data when required - lazy loading
load throws exception when data not found

10. Caching - First Level, SecondLevel, Query Cache


- First Level - associated with sessio object, enabled by default(no way to
disable), hibernate provided methods through which selected objects can be deleted
from cache or clear completely. Any object cached in a session will not be visible
to other session and when sesion closed, all cache is lost

- Second Level - By default disabled can be enabled through configuration. Provided


through EHCache(add it to pom), Add properties in hibernate configuration file -
Create EHCacheCOnfigurationFile -> diskstore(in case capacity of object to cache
increased, where to store), timetolive, diskbuffersize, maxentries etc. Finally in
Entity - @Cache(usage=CacheConcuurencyStrategy.READ_ONLY, region="employee")

11. @Transaction in SpringBoot


- Defines a boundary in which all operations performed must be persisted after the
boundary end is reached. Different types of transactions -> REQUIRED(crrates one if
none exists, REQUIRES_NEW(suspends earler and created new- not recommnded as it
bounds new transaction to new db connection which has a limit, NESTED(creates a
save point at the calling method), MANDATORY(Requires a trasnaction to be created
by parent), NEVER(Requires there is no transaction created earlier).
NOT_SUPPORTED(earlier transaction suspended and this executes without a transaction
boundary), SUPPORTS(can work either way - whether or not a transaction exists)

You might also like