Thursday, January 27, 2022

How to start with Hibernate Framework | From Dilsecodie

 



Let's understand about hibernate framework.

Hibernate is a library providing (ORM) mapping support to applications, libraries and other frameworks.

It also provides an implementation of the JPA  Framework. which is used in another spring framework

Hibernate JPA having some relationship :

1. @OneToOne: annotation can be used for one-to-one relationships.

 Its have two types of attributes: 

a. cascade 

b. fetch.

2. @OneToMany: This is a type of annotation that can be used for one-to-many relationships.   I contain two types of attributes: a. cascade b. fetch.

3. OneToOneRead: find(className, with primary id): This method takes two parameters first is the class name and the second one is the id which has to be retrieved data from the database.

4. OneToOneUpdate merge(): This method updates records, Method takes an object with an id. Records are updated this particular id.

5. OneToOneDelete remove(): This method removes a particular record. , Method takes an object with an id. Records are deleted this particular id.

Some hibernate package need to import for used in class:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

 SessionFactory is used to create for any given database, the application should have only one associated SessionFactory. The SessionFactory maintains services that Hibernate uses for all Sessions such as caches, connection pools, transaction systems for integration etc.

 Let's look for example using hibernate. 

public class SessionCreator extends HttpServlet - for session creator. 

public SessionCreator() {}  constructor.

Session - basically (org.hibernate.Session)A single-threaded, short-lived object conceptually modelling a PoEAA.  JPA is the Session is represented by EntityManager for it .

EntityTransaction is mainly used for control transaction system  for jdbc and jpa purposes ,in return its provide entity interface , its have object level transaction  management with it . 

for some example with hibernate jdbc modules like :
Query query = new Query().addCriteria(Criteria.where("status").is(1))
.addCriteria(Criteria.where("locale_lang").regex("US"));
Query is used to generate request with database .
addCriteria is used as same as where clause in jdbc its kind of filter 

For some other way to get complete data from table  we can use
Dilsecodie user= (Dilsecodie) sessionFactory.getCurrentSession().createCriteria(Dilsecodie.class)
.add(Restrictions.eq("username", emailId)).add(Restrictions.eq("isactive", Byte.valueOf("1"))).uniqueResult();

This example also gives us a unique result from the table.

Some other ways to get data : 
List<dilsecodiemap> dilseList = sessionFactory.getCurrentSession().createCriteria(Dilsecodie.class).
you may also take table data directly in list form. It provides you complete data from the table. 

Some other ways we can also use custom queries to get results  : 
sessionFactory.getCurrentSession().createQuery("UPDATE Dilsecodie  SET usableBal= usableBal")  

For spring we can auto wire  for session factory,
        @Autowired
SessionFactory sessionFactory;

Session factory provides sessions for hibernating in the spring framework.


Note *: we will update other information on hibernate keep following our thanks.





No comments:

Post a Comment