0% found this document useful (0 votes)
4 views

Hibernate_Mapping_raghu

The document provides an overview of Hibernate relationships, specifically focusing on 'is-a' and 'has-a' relationships, detailing the different mapping strategies such as Table Per Class, Table Per Subclass, and Table Per Concrete Class. It includes examples of Java classes and their corresponding Hibernate mapping configurations for both types of relationships. Additionally, it covers various collection mappings and annotations for managing relationships between entities in Hibernate.

Uploaded by

Bl Gocher
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Hibernate_Mapping_raghu

The document provides an overview of Hibernate relationships, specifically focusing on 'is-a' and 'has-a' relationships, detailing the different mapping strategies such as Table Per Class, Table Per Subclass, and Table Per Concrete Class. It includes examples of Java classes and their corresponding Hibernate mapping configurations for both types of relationships. Additionally, it covers various collection mappings and annotations for managing relationships between entities in Hibernate.

Uploaded by

Bl Gocher
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

RAGHU SIR NOTES: [note - see collection mapping in poto]

EXAMPLE OF :-HIBERNATE RELATION


-------------------------------
1- IS-RELATION
2- HAS-RELATION

1- IS-RELATION:- 3 TYPE OF TABLE CAN BE CREATE


----------------------------------------------
1- CREATE ONE TABLE / [TABLE PER CLASS]
2- CREATE CONNECTED TABLE / [TABLE PER SUB CLASS]
3- CREATE INDEPEDENT TABLE / [TABLE PER CONCREATE CLASS]
EX: 3 classes are there in is-a relation
ex:

public class person


{
pid
pname
}
public class Employee extends person
{
empsal
deptname
}
public class Student extend person
{
stfee
grade
}

1- CREATE ONE TABLE / [TABLE PER CLASS] op===> [ pid pname esal dept
stfee grade desc -(extra column)]
-----------------------------------------------------------------------------------
------------------------------------------
ex:-Mapping

</Hibernate-mapping>

< class name="Person" Table = "Common_Tab" Discreminator-value="PER" >


< id name = "pid" column = "pid" >
< property name = "pname" column = "pname" >

< subclass name = "Employee" Discreminator-value="Emp" >


< property name = "empsal" column = "empsal" >
< property name = "deptname" column = "deptname" >
</subclass>

< subclass name = "Student" Discreminator-value="STD" >


< property name = "stfee" column = "stfee" >
< property name = "grade" column = "grade" >
</subclass>

</class>

</Hibernate-mapping>

2- CREATE CONNECTED TABLE / [TABLE PER SUB CLASS] op===> [ pid-(pk) pname ],
[pid-fk esal dept ], [pid-fk stfee grade]
===================================================================================
=========================================================================
ex:mapping

ex:-Mapping

<Hibernate-mapping>

< class name="Person" Table = "Person_Tab" >


< id name = "pid" column = "pid" >
< property name = "pname" column = "pname" >

< joined-subclass name = "Employee" Table="Emp_Tab" >


< key-column="pidfk" <- extra column
< property name = "empsal" column = "empsal" >
< property name = "deptname" column = "deptname" >
<joined-subclass>

< joined-subclass name = "Student" Table="Stu_Tab" >


< key-column="pidfk" <- extra column
< property name = "stfee" column = "stfee" >
< property name = "grade" column = "grade" >
</joined-subclass>

< /class>
</Hibernate-mapping>

3- CREATE INDEPEDENT/SEPERATE TABLE / [TABLE PER CONCREATE CLASS] : op=> [pid


pname] [pid pname empsal dept] [pid pname stfee grade]
-----------------------------------------------------------------------------------
----------------------------------------------------------
ex:-Mapping

<Hibernate-mapping>

< class name="Person" Table = "Person_Tab" >


< id name = "pid" column = "pid" >
< property name = "pname" column = "pname" >

< union-subclass name = "Employee" "Emp_Tab" >


< property name = "empsal" column = "empsal" >
< property name = "deptname" column = "deptname" >
< /union-subclass>

< union-subclass name = "Student" "Stu_Tab" >


< property name = "stfee" column = "stfee" >
< property name = "grade" column = "grade" >
< /union-subclass>

< /class>
</Hibernate-mapping>
2- HAS-RELATION
===============

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

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

1 --- * [one _ many]


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

DEMO CLASSESS : Address , Employee


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

public class Employee


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

private Address addr; [ or use all annotaion ]


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

public class Adress


{
private int aid;
private String loc;
private int pin;

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

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


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

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


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

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

<Many-to-One name = "addr" class = "Address" column = "aid_fk" />

</class>

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

< id name = "aid" column = "aid" >


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

</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 = "addr" class = "Address" column = "aid_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 ="addr" 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 ="addr" Table = "EmpTable" Cascade = "All">


<key column = "aid_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();

}
}

raghu in sort [in parent Employee we have Address addr ref var so write in Employee
mapping]
===================================================================================
=======

1=> <Many-to-One name = "addr" class = "Address" column =


"aid_fk" />

2=> <one-to-One name = "addr" class = "Address" column = "aid_fk"


unique="true" />

3=> < Set name ="addr" cascade="all">


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

4=> < Set name ="addr" Table = "EmpTable" Cascade = "All">


<key column = "aid_fk">
<many-to_many class = "Address " column = "aid_fk"/>
</Set>

tutorialpoint in sort all maping: [in emp table "address" is the column]
---------------------------------

1= <many-to-one name = "address" class="Address" column = "address"


not-null="true"/>

2= <one-to-one name = "address" class="Address" column = "address"


unique="true" not-null="true"/>

3= <set name = "certificates" cascade="all">


<key column = "employee_id"/>
<one-to-many class="Certificate"/>
</set>

4= <set name = "certificates" cascade="save-update" table="EMP_CERT">


<key column = "employee_id"/>
<many-to-many column = "certificate_id" class="Certificate"/>
</set>

You might also like