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

Hibernate_Mapping_2_TP

The document provides an overview of various Hibernate mapping techniques including Many-to-One, One-to-One, One-to-Many, and Many-to-Many relationships between Employee and Address or Certificate entities. It includes SQL table creation scripts, Java class definitions, and Hibernate mapping configurations for each relationship type. Each section outlines the structure and implementation details necessary for establishing these relationships in a database context.

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)
2 views

Hibernate_Mapping_2_TP

The document provides an overview of various Hibernate mapping techniques including Many-to-One, One-to-One, One-to-Many, and Many-to-Many relationships between Employee and Address or Certificate entities. It includes SQL table creation scripts, Java class definitions, and Hibernate mapping configurations for each relationship type. Each section outlines the structure and implementation details necessary for establishing these relationships in a database context.

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

tutorialspoint:

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

1-Many-to-One Mappings: [MANY ADDRESS TO ONE EMPLOYEE]


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

A Object can be associated with multiple objects.


For example, the same address object can be associated with multiple employee
objects.

TABLE:
create table EMPLOYEE (
id INT NOT NULL auto_increment,
first_name VARCHAR(20) default NULL,
last_name VARCHAR(20) default NULL,
salary INT default NULL,
addid INT NOT NULL,
PRIMARY KEY (id)
);

TABLE:
create table ADDRESS (
id INT NOT NULL auto_increment,
street_name VARCHAR(40) default NULL,
city_name VARCHAR(40) default NULL,
state_name VARCHAR(40) default NULL,
zipcode VARCHAR(10) default NULL,
PRIMARY KEY (id)
);

EMPLOYEE.JAVA
--------------
public class Employee{
private int id;
private String firstName;
private String lastName;
private int salary;
private Address address;

// SETT/GETTER
}

Address.java
------------
public class Address{
private int id;
private String street;
private String city;
private String state;
private String zipcode;

gett/sett
}

<hibernate-mapping>
<class name = "Employee" table = "EMPLOYEE">

<meta attribute = "class-description">


This class contains the employee detail.
</meta>

<id name = "id" type = "int" column = "id">


<generator class="native"/>
</id>

<property name = "firstName" column = "first_name" type = "string"/>


<property name = "lastName" column = "last_name" type = "string"/>
<property name = "salary" column = "salary" type = "int"/>
<many-to-one name = "address" column = "address"
class="Address" not-null="true"/>

</class>

<class name = "Address" table="ADDRESS">

<meta attribute = "class-description">


This class contains the address detail.
</meta>

<id name = "id" type = "int" column = "id">


<generator class="native"/>
</id>

<property name = "street" column = "street_name" type = "string"/>


<property name = "city" column = "city_name" type = "string"/>
<property name = "state" column = "state_name" type = "string"/>
<property name = "zipcode" column = "zipcode" type = "string"/>

</class>

</hibernate-mapping>

2- Hibernate - One-to-One Mappings: [ONE ADDRESS TO ONE EMPLOYEE]


===================================
A one-to-one association is similar to many-to-one association with a difference
that the column will be set as unique.

EMLOYEE
-------
create table EMPLOYEE (
id INT NOT NULL auto_increment,
first_name VARCHAR(20) default NULL,
last_name VARCHAR(20) default NULL,
salary INT default NULL,
address INT NOT NULL,
PRIMARY KEY (id)
);

ADRESS:
-------
create table ADDRESS (
id INT NOT NULL auto_increment,
street_name VARCHAR(40) default NULL,
city_name VARCHAR(40) default NULL,
state_name VARCHAR(40) default NULL,
zipcode VARCHAR(10) default NULL,
PRIMARY KEY (id)
);

public class Employee{


private int id;
private String firstName;
private String lastName;
private int salary;
private Address address;

// sett / gett
}

set to unique constraint and rest of the mapping file will remain as it was in case
of many-to-one association.

<hibernate-mapping>
<class name = "Employee" table = "EMPLOYEE">

<id name = "id" type = "int" column = "id">


<generator class="native"/>
</id>

<property name = "firstName" column = "first_name" type = "string"/>


<property name = "lastName" column = "last_name" type = "string"/>
<property name = "salary" column = "salary" type = "int"/>
<one-to-one name = "address" column = "address" unique="true"
class="Address" not-null="true"/>

</class>

<class name = "Address" table="ADDRESS">

<id name = "id" type = "int" column = "id">


<generator class="native"/>
</id>

<property name = "street" column = "street_name" type = "string"/>


<property name = "city" column = "city_name" type = "string"/>
<property name = "state" column = "state_name" type = "string"/>
<property name = "zipcode" column = "zipcode" type = "string"/>

</class>

</hibernate-mapping>

3-Hibernate - One-to-Many Mappings: [ONE EMPLOYEE TO MANY CERTIFICATE]


----------------------------------
assume each employee can have one or more certificate associated with him/her.
A One-to-Many mapping can be implemented using a Set java collection that does not
contain any duplicate element.

create table EMPLOYEE (


id INT NOT NULL auto_increment,
first_name VARCHAR(20) default NULL,
last_name VARCHAR(20) default NULL,
salary INT default NULL,
PRIMARY KEY (id)
);

create table CERTIFICATE (


id INT NOT NULL auto_increment,
certificate_name VARCHAR(30) default NULL,
employee_id INT default NULL,
PRIMARY KEY (id)
);

EMPLOYEE.JAVA
-------------
public class Employee {
private int id;
private String firstName;
private String lastName;
private int salary;
private Set certificates;

CERTIFICATE.JAVA
----------------
public class Certificate {
private int id;
private String name;
}

<hibernate-mapping>
<class name = "Employee" table = "EMPLOYEE">
<id name = "id" type = "int" column = "id">
<generator class="native"/>
</id>

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


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

<property name = "firstName" column = "first_name" type = "string"/>


<property name = "lastName" column = "last_name" type = "string"/>
<property name = "salary" column = "salary" type = "int"/>

</class>

<class name = "Certificate" table = "CERTIFICATE">

<id name = "id" type = "int" column = "id">


<generator class="native"/>
</id>

<property name = "name" column = "certificate_name" type = "string"/>

</class>

</hibernate-mapping>

4- Hibernate - Many-to-Many Mappings :


--------------------------------------
assume each employee can have one or more certificate associated with him/her and a
similar certificate can be associated with more than one employee.
A Many-to-Many mapping can be implemented using a Set java collection that does not
contain any duplicate element.

create table EMPLOYEE (


id INT NOT NULL auto_increment,
first_name VARCHAR(20) default NULL,
last_name VARCHAR(20) default NULL,
salary INT default NULL,
PRIMARY KEY (id)
);

create table EMP_CERT (


employee_id INT NOT NULL,
certificate_id INT NOT NULL,
PRIMARY KEY (employee_id,certificate_id)
);

public class Employee


{
private int id;
private String firstName;
private String lastName;
private int salary;
private Set certificates;
}

public class Certificate


{
private int id;
private String name;
}

<hibernate-mapping>
<class name = "Employee" table = "EMPLOYEE">

<id name = "id" type = "int" column = "id">


<generator class="native"/>
</id>

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


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

<property name = "firstName" column = "first_name" type = "string"/>


<property name = "lastName" column = "last_name" type = "string"/>
<property name = "salary" column = "salary" type = "int"/>

</class>

<class name = "Certificate" table = "CERTIFICATE">

<id name = "id" type = "int" column = "id">


<generator class="native"/>
</id>

<property name = "name" column = "certificate_name" type = "string"/>

</class>

</hibernate-mapping>

==========================================IN SORT============================
insort
------

MANY TO ONE : [ [MANY ADDRESS TO ONE EMPLOYEE]]


-----------------------------
<hibernate-mapping>

<class name = "Employee" table = "EMPLOYEE">

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


not-null="true"/>
</class>

</hibernate-mapping>

ONE TO ONE : [ONE ADDRESS TO ONE EMPLOYEE]


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

<class name = "Employee" table = "EMPLOYEE">

<many-to-one name = "address" column = "address" unique="true"


class="Address" not-null="true"/>

</class>

ONE TO MANY : [ONE EMPLOYEE TO MANY CERTIFICATE]


-----------
<hibernate-mapping>
<class name = "Employee" table = "EMPLOYEE">

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


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

</class>

MANY TO MANY : [ MANY EMP____MANY CERTIFICATES]


------------------
<hibernate-mapping>
<class name = "Employee" table = "EMPLOYEE">

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


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

</class>
==============================================JAVA4S.TUTORIALS=====================
=============

You might also like