0% found this document useful (0 votes)
2 views5 pages

Hibernate Mapping

The document outlines various mapping types in Hibernate, including non-collection and collection mappings for entities like Address and Employee. It provides examples of one-to-one, one-to-many, and many-to-many relationships, along with corresponding Java class structures and Hibernate mapping configurations. Additionally, it includes a sample program demonstrating the use of Hibernate for saving entities in a many-to-many relationship.

Uploaded by

Bl Gocher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Hibernate Mapping

The document outlines various mapping types in Hibernate, including non-collection and collection mappings for entities like Address and Employee. It provides examples of one-to-one, one-to-many, and many-to-many relationships, along with corresponding Java class structures and Hibernate mapping configurations. Additionally, it includes a sample program demonstrating the use of Hibernate for saving entities in a many-to-many relationship.

Uploaded by

Bl Gocher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

RAGHU SIR NOTES:

-----------------

MAPPING TYPE:
Non_Collection:
---------------
* --- 1 [many _ one]
1 --- 1 [one _ one]

Collection:
-----------

1 --- * [one _ many]


* --- * [many _ many]
-----------------------------------

DEMO CLASSESS : Address , Employee


=====================================
public class Adress
{
private int aid;
private String loc;
private int pin;

public class Employee


{
private int eid
private String eName;
private int esal;

private Address addr; [ or use all annotaion ]


@OneToMany(mappedBy="adid_fk", cascade=CascadeType.ALL)

Non_Collection:
---------------

* --- 1 [many _ one] [ Many Emplyee have one Address]


=======================

Table : 1 Employee [ eid (pk) | ename | esal | adid (fk)] <=== 2


Address = [ aid (pk) | loc | pin ]

Mapping:
========
<Hibernate-mapping package="com.app">

< class name="Address" Table = "AddressTab" >

< id name = "adid" column = "adid" >


< property name = "location" column = "loc" >
< property name = "pin" column = "pin" >

</class>

< class name="Employee" Table = "EmpTable" >

< id name = "eid" column = "eid" >


< property name = "ename" column = "ename" >
< property name = "esal" column = "esal" >

<Many-to-One name = "adid" class = "Address" column = "adid_fk" />

</class>

</Hibernate-mapping>

[ 1--- 1 one_ one] [ one Emplyee have only one Address] [add unique=true]
=======================

< class name="Employee" Table = "EmpTable" >

< id name = "eid" column = "eid" >


< property name = "ename" column = "ename" >
< property name = "esal" column = "esal" >

<one-to-One name = "adid" class = "Address" column = "adid_fk"


unique="true" />

</class>

-----------------------------------------------------------------------------------
--------------------------

Collection:
-----------

1 --- * [one _ many] [one employee has many address] [set]

then in java property will be in class like -

public class Employee


{
--
--
--

private Set <Address> addr;


}
Mapping:
========
<Hibernate-mapping package="com.app">

< class name="Employee" Table = "EmpTable" >

< id name = "eid" column = "eid" >


< property name = "ename" column = "ename" >
< property name = "esal" column = "esal" >

< Set name ="address" cascade="all">


<key column = "adid_fk">
<one-to_many class = "address "/>
</Set>
</class>

</Hibernate-mapping>

* --- * [many _ many] [many employee can have many addresses] [set]
-----------------------------------------------------------------------

classes will be same

Mapping-changes in Employee

<Hibernate-mapping package="com.app">

< class name="Employee" Table = "EmpTable" >

< id name = "eid" column = "eid" >


< property name = "ename" column = "ename" >
< property name = "esal" column = "esal" >

< Set name ="address" Table = "EmpTable" Cascade = "All">


<key column = "adid_fk">
<many-to_many class = "address " column =
"adid_fk"/>
</Set>
</class>

</Hibernate-mapping>

===================================================================================
=========================================================================
[Hibernate Annotations]

@Entity
@Table(name = "EMPLOYEE")
public class Employee {
@Id @GeneratedValue
@Column(name = "id")
private int id;

@Column(name = "first_name")
private String firstName;

@Column(name = "last_name")
private String lastName;

@Column(name = "salary")
private int salary;
}

====================

full programm:

public class OurLogic {

public static void main(String args[])


{

Configuration cfg = new Configuration();


cfg.configure("hibernate.cfg.xml");

SessionFactory factory = cfg.buildSessionFactory();


Session session = factory.openSession();

Student s1=new Student();


s1.setStudentId(100);
s1.setStudentName("James");
s1.setMarks(98);

Course c1=new Course();


c1.setCourseId(500);
c1.setCourseName("Hibernate");

Transaction tx = session.beginTransaction();

session.save(s1);
session.save(c1);

tx.commit();

session.close();
System.out.println("Many To Many Bi-Directional is Done..!!");
factory.close();

}
}

You might also like