Double Bonanza Offer - Upto 30% Off + 1 Self Paced Course Free | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
Component Mapping
Component Mapping:

In part Component mapping, we will delineate ward question as a segment. A part is a question that is put away as an value instead of substance reference. This is principally utilized if the needy protest does not have essential key. It is utilized as a part of instance of organization (HAS-A connection), that is the reason it is named as segment. We should see the class that have HAS-A relationship.

One to One Mapping in Hibernate by many-to-one:
  • By many-to-one element
  • By one-to-one element
 Persistent classes:
Employee.java
 package com.javaspot;public class Employee {private int employeeId;private String name,email;private Address address;//setters and getters}

Address.java

package com.javaspot;public class Address {private int addressId;private String addressLine1,city,state,country;private int pincode;private Employee employee;//setters and getters}2) Mapping files for the persistent classes

employee.hbm.xml

<class name="com.javatpoint.Employee" table="emp211">class="increment">class>

address.hbm.xml

<class name="com.javatpoint.Address" table="address211">class="increment">class>3) Configuration file
hibernate.cfg.xml
updateorg.hibernate.dialect.Oracle9Dialectjdbc:oracle:thin:@localhost:1521:xesystemoracleoracle.jdbc.driver.OracleDriver4) User classes to store and fetch the data
Store.java
package com.javaspot;import org.hibernate.cfg.*;import org.hibernate.*;public class Store {public static void main(String[] args) {Configuration cfg=new Configuration();cfg.configure("hibernate.cfg.xml");SessionFactory sf=cfg.buildSessionFactory();Session session=sf.openSession();Transaction tx=session.beginTransaction();Employee e1=new Employee();e1.setName("Ravi Malik");e1.setEmail("ravi@gmail.com");Address address1=new Address();address1.setAddressLine1("G-21,Lohia nagar");address1.setCity("Ghaziabad");address1.setState("UP");address1.setCountry("India");address1.setPincode(201301);e1.setAddress(address1);address1.setEmployee(e1);session.persist(e1);tx.commit();session.close();System.out.println("success");}}

Fetch.java

package com.javaspot;import java.util.Iterator;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class Fetch {public static void main(String[] args) {Configuration cfg=new Configuration();cfg.configure("hibernate.cfg.xml");SessionFactory sf=cfg.buildSessionFactory();Session session=sf.openSession();Query query=session.createQuery("from Employee e");List list=query.list();Iterator itr=list.iterator();while(itr.hasNext()){Employee emp=itr.next();System.out.println(emp.getEmployeeId()+" "+emp.getName()+" "+emp.getEmail());Address address=emp.getAddress();System.out.println(address.getAddressLine1()+" "+address.getCity()+" "+address.getState()+" "+address.getCountry());}session.close();System.out.println("success");}}Transaction Management 

An   transaction basically speaks to a unit of work. In such case, on the off chance that one stage falls flat, the entire exchange comes up short (which is named as atomicity). An exchange can be depicted by ACID properties (Atomicity, Consistency, Isolation and Durability.

Transaction Management

 Transaction Interface

  1. void begin()
  2. void commit()
  3. void rollback()
  4. void setTimeout(int seconds)
  5. boolean isAlive()
  6. void registerSynchronization(Synchronization s)
  7. boolean wasCommited()
  8. boolean wasRolledBack()
Hibernate Query Language:

Hibernate Query Language (HQL) is same as SQL (Structured Query Language) however it doesn't relies upon the table of the database. Rather than table name, we utilize class name in HQL. So it is database autonomous inquiry dialect.

Advantage of HQL:

  • Independent of database
  • Polymorphic queries Will be supported
  • Learn easy for Java Programmer

Query Interface

It is a protest arranged portrayal of Hibernate Query. The question of Query can be gotten by calling the createQuery() technique Session interface.
  • public int executeUpdate()
  • public List list()
  • public Query setFirstResult(int rowno)
  • public Query setMaxResult(int rowno)
  • public Query setParameter(int position, Object value)
  • public Query setParameter(String name, Object value)