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

<?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 file

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”>

<!– 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 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<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 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)

 

Drop Us A Query

Trending Courses
  • Python and Django Online Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

  • Tableau Online Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • MicroStrategy Online Training
  • Micro Strategy is one of the few independent and publicly trading Business Intelligence software provider in the market. The firm is operational in 27 Countries around the globe.

  • PEGA (PRPC) 7.2 Certification Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).

  • Workday Online Training
  • Workday specialises in providing Human Capital Management, Financial Management and payroll in online domain.It is a major web based ERP software vendor.

  • Power BI Online Training
  • Power BI is business analytics service by Microsoft. With Power BI, end users can develop reports and dashboards without depending on IT staff or Database Administrator.


100% Secure Payments. All major credit & debit cards accepted.