Hibernate ORM
priviously we used to create Data access Object then write sql query to perform then take each value from object and then perform SQL operation on it.
> Using Hibernate we don't need to create DAO object for storing data and then use query in it.
here it is done automatically by hibernate all we need to do is while creating class we map it with table in database . In backend Hibernate creates JDBC query for us and performs actions as asked by us.
> Implementation in Eclipse:
adding hibernate and sql-connector dependency.
After copying dependency update Maven project so it will download all required dependency.
> Configuring Hibernate XML file :
will have to create xml file with name : hibernate.cfg.xml
this will be stored in com/src/java folder so our program can automatically detect it else will have to proper path for it.
then will have to provide following connection properties for our program
- DB driver name
- connection url :hostname ,port ,DB name
- connection Username
- connection password
- dialect : here will be sql dialect (i.e the language for DB)
- hbm2ddl.auto is here to automatically create and update table
- show_sql is for showing the statement that hibernate has executed in backend
we have many more properties but for the time being we will use only these
> Implementing Hibernate using Annotation :
here we have annotated class with @Entity this will be treated as entity/table now hibernate will make table with same name of the class in DB.
@Id is used to make Id variable as primary key
then we will need to make changes in Hibernate XML file to make student class as entity/table.
for inserting data in DB.
If we dont specify @Table with name attribute the table in DB will be created by name Address by default .Now here it will be student_address
length parameter is for giving maximum size to columns value
@Transient is being used so that there should be no column in Table of 'x' variable.
In @Temporal TemporalType.DATE is to store only the Date not the time stamp or time in DB.
@Lob is for large object for image type data.
Since image is being stored in form of bytes so we are reading it using byte array.
also we will need to map address class in hibernate configuration file.
here for property hbm2ddl.auto property if we use:
- create: it will drop and create table each time we execute our code.
- update : it will only create table if it is not found in DB.
> Fetching Data from DB
can be done by : get() & load() and passing entity and primary key as parameter.
in get() if same object is called multiple times from DB then it will check in session memory weather the object is present or not if not then it will fire select query else it will return object from session cache.
> @Embeddable annotation is used to store variable of different class in a table :
here in above ex we have to store Certificate class object in Student table so here we will use @Embeddable annotation for Certificate class so we can store its data inside Student table.
> MAPPING in Hibernate :
> ONE to ONE Mapping:
here we have to 2 table question and answers.
In one to one we can have :
- one answer for one question (Uni-directional).
- one answer for one question & one question for one answer (Bi-directional)
So here aid will act as a foreign key of questions table (aid is primary key in answers table).
Similarly for bi-directional we can create variable for question table and annotate it as shown.
but by this both the table will have foreign keys for both the other table so one more column ,
So to resolve this we can use "mappedBy" parameter it will be bi directional too :
to remove redundancy (extra column)
So now by this our tables are bi - directional but the extra column won't be created
So here the mapped by used in Answer class is to map answer object of question class means there will be foreign key column only in Questions class and not in Answer class.
> One to Many & Many to One: here one questions can have many answers & many answers can have one question.
Similarly in this also we will use mappedBy as above OneToMany to remove redundancy (extra table will be created here having primary keys of both the tables)
So here as we have used mappedBy in questions object of answers class i.e here extra foreign key column will be created in Answer class.
>Many to Many: here one or more project can be assigned to one or many employee.
It will create one new extra table which will contain mapping of respective primary keys and foreign key.
here since we have used mappedBy for projects variable of Emp class so it will create only 1 extra table wrt to emp table.
> Fetch Techniques (LAZY loading & EAGER loading)
Here taking example of class Questions having answer class object in it :
So in case of EAGER loading answers class will be loaded as we create/get questions class object.
but in case of LAZY loading until we don't create/get answers object it won't be loaded.
So manualy we can make fetch type LAZY or EAGER by default its lazy.
> Hibernate Objects States
> HQL : Hibernate Query Language :
But when we will have to fetch complex queries which is using multiple condition or joins for that we will need query language for executing it.
we can use both SQL and HQL here :
the main difference between HQL and SQL is:
here in HQL the Student is name of java entity class where as in SQL Student is name of table in database
here in query we are using s as alias of Student class.
here we are using HQL and creating query by using session object method create query and in where condition condition parameters are being set by setParameter() method.
then for performing operations fetching query result in list since multiple values are coming here.
similarly we can do it for updating and deleting the query.
for updating and deleting we will have to begin and close the transaction.
and here we will use executeUpdate() function to make changes in DB.
strikethrough on the Query and methods of the program means it was with old version now it has been replaced with new API and methods
> Pagination in hibernate :
used to display limited number of records from table
here setFirstResult() is the method used to give start point to records
setMaxResults() used for providing number of records per page
here it will start from 0th record and display total 5 records.
and in backend it is using limit parameter in sql query
> Implementation SQL
Since it is array of object so we can fetch data using their index value
> Cascading in hibernate :
By using cascade in mapping we can perform similar operation for related entities of any class. Example
as here we can see we have just saved question here so if we dont use cascade type ALL for answers we will have to manually save all answers ( i.e s.save(a1) , s.save(a2) ... an so on) here in database.
as we have written CascadeType ALL so all the CRUD operation performed on question class object will be performed on its related answer object.
> Caching in Hibernate:
after that 2 time it will be available in the cache memory
- First Level Caching
here we can see query is being executed only once
second time it is accessing student object for id 12424 from session cache memory.
2. Second Level Caching: can be done by cache providers
here we are using eh cache .
so firstly we will need to download its dependency jars using pom.xml.
here we will use same version for hibernate-ehcache as of our hibernate
also will have to provide these 2 property in hibernate configuration file.
also provide @Cacheable and @Cache annotation for the entity class for which we want to create cache memory.
Comments
Post a Comment