Component Mapping
Last updated on Jun 15, 2020
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 classesemployee.hbm.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.javatpoint.Employee" table="emp211"> <id name="employeeId"> <generator class="increment"></generator> </id> <property name="name"></property> <property name="email"></property> <many-to-one name="address" unique="true" cascade="all"></many-to-one> </class> </hibernate-mapping>address.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.javatpoint.Address" table="address211"> <id name="addressId"> <generator class="increment"></generator> </id> <property name="addressLine1"></property> <property name="city"></property> <property name="state"></property> <property name="country"></property> </class> </hibernate-mapping> 3) Configuration filehibernate.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"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="hbm2ddl.auto">update</property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property> <property name="connection.username">system</property> <property name="connection.password">oracle</property> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <mapping resource="employee.hbm.xml"/> <mapping resource="address.hbm.xml"/> </session-factory> </hibernate-configuration> 4) User classes to store and fetch the dataStore.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<Employee> list=query.list(); Iterator<Employee> 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 ManagementAn 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 Interface
- void begin()
- void commit()
- void rollback()
- void setTimeout(int seconds)
- boolean isAlive()
- void registerSynchronization(Synchronization s)
- boolean wasCommited()
- boolean wasRolledBack()
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)

