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)

 

Subscribe
to our newsletter

Drop Us A Query

Trending Courses
  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • Deep learning course
    Deep Learning Course
  • Artificial Intelligence, Deep mastering (DL) is completely about, many levels of Representation and sort of abstraction. That guide to design a sense of Information like Images, sound and text format.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

  • devops with azure
    Devops with Azure Training
  • As we Know Azure DevOps is a Bunch of Services, in guiding Developers. It contains CI/CD, pipelines, code Repositories, Visual Reporting Tools and more code management with version control.


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