Skip to main content

Posts

Showing posts matching the search for hibernate

Hibernate Introduction

It is a framework of java that contains a set of libraries to provide a secure data access layer.it has complete ORM (Object-relational mapping) to map the complete database table using POJO Classes. It provides SQL LESS (NoSQL) query to perform a database operation, it provides a persistent object model, criteria, HQL(Hibernate query language) to perform the database-related tasks. Hibernate provide a strong approach to data communication because we can map complete database structure and relationship using the hibernate framework. 1 ) Create a table, primary key, foreign key, identity using front-end 2) Data insertion, updating, deletion, data selection, join operation, data filtering, table mapping, and table relationship from the application. Hibernate provides better security as compared to normal SQL queries because it protects from SQL Injection problems. SQL Injection means hacking the database using  SQL Query parameters. that can be easily managed by normal ...

CRUD Operation using Hibernate Session1

hibernate.cfg.xml:- <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hdb1</property> <property name="connection.username">root</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hbm2ddl.auto">update</property> <property name="show_sql">true</property>  <mapping resource="hibernate.hbm.xml"></mapping> </session-factory> </hibernate-configuration> hibernate.hbm.xml:- <?xml versi...

Hibernate 5 Tutorials

Hibernate 5 Tutorials  Now Hibernate 3 and 4 are deprecated and Industry prefer Hibernate 5 and Hibernate 6. You  have knowledge of Hibernate 5 also if you have knowledge Hibernate 3 and four. Many Updating added in Hibernate 5 Now I am explaining Step by Step 1)  Open Eclipse Create Maven Project, Select Internal under Category and select QuickStart archetype 2)  Add Dependency under pom.xml file of hibernate and MySQL Connector <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">   <modelVersion>4.0.0</modelVersion>   <groupId>com.scs</groupId>   <artifactId>Hibernate5</artifactId>   <version>0.0.1-SNAPSHOT</version>   <packaging>jar</packaging>   <name>Hibernate5</name>   <url>http://maven.a...

Join Example using one to many in hibernate

Step1st:-  Create POJO Class for Vendor and Customer:- package scs; import java.util.Set; public class Vendor {    private int vid;    private String vname;    private Set children;   public Set getChildren() { return children; } public void setChildren(Set children) { this.children = children; } public int getVid() { return vid; } public void setVid(int vid) { this.vid = vid; } public String getVname() { return vname; } public void setVname(String vname) { this.vname = vname; }   } package scs; public class Customer {   private int customerid;   private String customername;   private int fid; public int getCustomerid() { return customerid; } public void setCustomerid(int customerid) { this.customerid = customerid; } public String getCustomername() { return customername; } public void setCustomername(String customername) { this.customername = customername; ...

Hibernate cache Code

Hibernate uses the cache concept to reduce the roundtrip from the database server if we want to increase the performance of the application. Type of cache 1)  Primary Cache:-  It will be created implicitly. 2)  Secondary Cache:-  It will be created explicitly. create ehcache.xml <?xml version="1.0"?> <ehcache> <defaultCache maxElementsInMemory="100" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="200" /> <cache name="scs.Job" maxElementsInMemory="100" eternal="false" timeToIdleSeconds="5" timeToLiveSeconds="200" /> </ehcache> Code under hibernate.cfg.xml:- <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-f...

Join in Hibernate Example, Hibernate Relationship using One to Many and Many to one

Join is used to merge the columns, in the case of Hibernate we will create a relationship or mapping concept to perform the join operation:- Type of relationship or association One to Many -->   One dept has multiple employees Many to One ---> Multiple employees belonging from Single dept Many to Many ---> Multiple Employee belonging from Multiple Dept. One to One  ------->   One Dept to one Employee Example of Join Syntax:- Implement Many to one using Two Different Entity Class Dept and Employee:- Code of Dept Class:- package scs; import java.util.Set; import javax.persistence.*; @Entity @Table(name="tbl_dept") public class Dept { @Id private int deptid; @Column private String deptname; public int getDeptid() { return deptid; } public void setDeptid(int deptid) { this.deptid = deptid; } public String getDeptname() { return deptname; } public void setDeptname(String deptname) { this.deptname = deptname; } } Code of Employee Class:- package scs; imp...

Criteria and Projection Example in Hibernate

Criteria is used to get complete data from database and return complete object ,It is used to apply condition in object using Restriction class and Criterion Interface. It is only for Data Selection  not for Data Insertion ,Updation and Deletion. Code of  Data Selection :- import java.util.Iterator; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Restrictions; import scs.Student; public class CriteriaExample { public static void main(String[] args) { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml");         SessionFactory sf = cfg.buildSessionFactory();         Session s =sf.openSession();         Criteria crt = s.createCriteria(Student.class);   ...

Hibernate Annotation Example

Annotation is used to change the behavior of a class, method, and data member of the class using a predefined attribute. Without using the hibernate mapping file we can map the database table using an annotation class. The annotation will be defined under the POJO Class. @Table:-  this annotation is used to map the database table from class @Column:-  this annotation is used to map the database table column @id:-   it is used to define the primary key column of the table @Entity:-  It is used to provide annotation to implement database schema if we use the Annotation Mapping class then we will define the mapping class under the hibernate.cfg.xml file < mapping class= "packagename.Pojoclassname" / > All hibernate annotations exist under javax.persistent package. means javax.persistence jar file should be present in your project lib folder. Step First:- Create Pojo class with annotation package scs; import javax.persistence.*; @Entity @Table(name="tbl_stud...

Criteria in Hibernate

Criteria return complete objects after data selection, it is the predefined interface of Hibernate which contains multiple methods to perform the conditional operation. For example, if we want to select the record of students whose fees are less than 50000 then we will use Criteria Object. It is the best way to select data from the database. The main difference between criteria and HQL is that criteria return complete objects and HQL return partial objects. We can perform data insertion, update, deletion, and selection using HQL but using Criteria we can select data only. Syntax of Criteria in Hibernate? Criteria q = session.createCriteria(PojoClassname);  //Vendor.class List lst = q.list(); Iterator it = lst.iterator(); while(it.hasNext()) {     PoJO v=(POJO)it.next();    out.print(v.getterOfPojo()+" "+v.getterOfPojo()+"<br>"); } What are Restrictions? It is another interface that is used to apply condition on Criteria Object, Restrictions Class pr...

Spring MVC With Hibernate Example, Database connectivity in Spring MVC + Hibernate + MYSQL

Step1s:- Create Database and Table. database name hiberdb and tablename employee(empid,empname,jo) Step2nd:- Create hibernate.cfg.xml:- <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hiberdb</property> <property name="connection.username">root</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <mapping class="com.scs.dao.Emp"></mapping> </session-factory> </hibernate-configuration> Step3rd:- Create POJO or BEAN Class under package com.scs.dao packag...

Hibernate Interview Question for Experienced

Q1 What is Hibernate Architecture? Q2 What is annotation in Hibernate, provide the name of all annotation? Q3  What is the relationship in hibernate explain all relationships with annotation? Q4  what is HQL? Q5  What is the Criteria, Criterion, Restrictions, Projection, Projections, ProjectionSet? Q6  Hibernate Join? Q7  What is SQL query,namedquery ? Q8  How we use Pagination in Hibernate Q9 Hibernate Cache primary and secondary? Q10 Difference between get() and load()? Q11  Difference between save() and persist() in Hibernate? Q12  Difference between merge() and update() in Hibernate?