举例讲解Java的Hibernate框架中的多对一和一对多映射

时间:2021-05-19

多对一(Many-to-One)映射
多对一(many-to-one)关联是最常见的关联关系,其中一个对象可以与多个对象相关联。例如,一个相同的地址对象可以与多个雇员的对象相关联。

定义RDBMS表:
考虑一个情况,我们需要员工记录存储在EMPLOYEE表,将有以下结构:

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));

此外,许多员工都可以有相同的地址,所以这种关联可以使用许多一对一的关联呈现。我们将存储地址相关的信息在一个单独的表,该表具有以下结构:

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));

同时创建RBDMS表,并让他们准备下一个实现。

定义POJO类:
让我们实现一个POJO类员工将被用于保存与EMPLOYEE表的对象和其地址类型的变量。

import java.util.*;public class Employee{ private int id; private String firstName; private String lastName; private int salary; private Address address; public Employee() {} public Employee(String fname, String lname, int salary, Address address ) { this.firstName = fname; this.lastName = lname; this.salary = salary; this.address = address; } public int getId() { return id; } public void setId( int id ) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName( String first_name ) { this.firstName = first_name; } public String getLastName() { return lastName; } public void setLastName( String last_name ) { this.lastName = last_name; } public int getSalary() { return salary; } public void setSalary( int salary ) { this.salary = salary; } public Address getAddress() { return address; } public void setAddress( Address address ) { this.address = address; }}

我们需要定义相应的地址表,这样地址对象可以存储和检索到地址表中的另一个POJO类。

import java.util.*;public class Address{ private int id; private String street; private String city; private String state; private String zipcode; public Address() {} public Address(String street, String city, String state, String zipcode) { this.street = street; this.city = city; this.state = state; this.zipcode = zipcode; } public int getId() { return id; } public void setId( int id ) { this.id = id; } public String getStreet() { return street; } public void setStreet( String street ) { this.street = street; } public String getCity() { return city; } public void setCity( String city ) { this.city = city; } public String getState() { return state; } public void setState( String state ) { this.state = state; } public String getZipcode() { return zipcode; } public void setZipcode( String zipcode ) { this.zipcode = zipcode; } }

定义Hibernate映射文件:
开发我们的映射文件,可指示Hibernate如何定义的类映射到数据库表。<many-to-one>进行元素将被用来定义规则建立Employee和Address实体之间的多对一关系。

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://mit(); }catch (HibernateException e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } }}

编译和执行:

$java ManageEmployee.......VARIOUS LOG MESSAGES WILL DISPLAY HERE........First Name: Manoj Last Name: Kumar Salary: 4000Certificate: MBACertificate: PMPCertificate: MCAFirst Name: Dilip Last Name: Kumar Salary: 3000Certificate: BCACertificate: BAFirst Name: Manoj Last Name: Kumar Salary: 5000Certificate: MBACertificate: PMPCertificate: MCA

如果检查员工和证书表,就应该记录下了:

mysql> select * from employee;+----+------------+-----------+--------+| id | first_name | last_name | salary |+----+------------+-----------+--------+| 1 | Manoj | Kumar | 5000 |+----+------------+-----------+--------+1 row in set (0.00 sec)mysql> select * from certificate;+----+------------------+-------------+| id | certificate_name | employee_id |+----+------------------+-------------+| 1 | MBA | 1 || 2 | PMP | 1 || 3 | MCA | 1 |+----+------------------+-------------+3 rows in set (0.00 sec)

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章