使用注解的Hibernate one-to-many映射
Posted 告别的时代
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用注解的Hibernate one-to-many映射相关的知识,希望对你有一定的参考价值。
One to many映射关系指的是两个实体间一个实体可以和多个实体有关联关系,但是多的这一端只能和一的这一端的一个实例有关系。它是一个1 到 n的关系。例如在任何的公司员工可以注册多个银行账户,一个银行账户只能和一个员工相关联,在这篇文章中我们将会学习怎么在Hibernate3中建立这种映射关系。
问题陈述
我们要写两个实体一个是Employee实体另一个是Account实体,这样多个银行账户就可以和一个员工关联了,但是这些账户不能被两个或以上的用户共享。
设计解决方案
这种问题可以使用两种方式解决。一种方式是在Account表中设置一个外键EMPLOYEE_ID,这一列指向Employee表的主键,这种方式没有两个账号可以和多个用户相关联,显然,为了完成这种限制,账号应该是独特的。另一种方式是建立一个连接表,比如说是叫EMPLOYEE_ACCOUNT,这个表有两列,EMP_ID作为EMPLOYEE表中主键的外键,对于ACCOUNT_ID也是这种情况。
使用外键连接
这种方式,两个实体都要负责建立关系并维护这种关系,EMPLOYEE实体应该申明的关系是one to many,Account实体应该声明的关系是many to one。首先来看一下关系设计:
EMPLOYEE实体
- package hibernate.test.oneToMany.foreignKeyAsso;
- import java.io.Serializable;
- import java.util.Set;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- import javax.persistence.UniqueConstraint;
- @Entity(name = "ForeignKeyAssoEntity")
- @Table(name = "Employee", uniqueConstraints = {
- @UniqueConstraint(columnNames = "ID"),
- @UniqueConstraint(columnNames = "EMAIL") })
- public class EmployeeEntity implements Serializable {
- private static final long serialVersionUID = -1798070786993154676L;
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "ID", unique = true, nullable = false)
- private Integer employeeId;
- @Column(name = "EMAIL", unique = true, nullable = false, length = 100)
- private String email;
- @Column(name = "FIRST_NAME", unique = false, nullable = false, length = 100)
- private String firstName;
- @Column(name = "LAST_NAME", unique = false, nullable = false, length = 100)
- private String lastName;
- @OneToMany(cascade=CascadeType.ALL)
- @JoinColumn(name="EMPLOYEE_ID")
- private Set<AccountEntity> accounts;
- public Integer getEmployeeId() {
- return employeeId;
- }
- public void setEmployeeId(Integer employeeId) {
- this.employeeId = employeeId;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public Set<AccountEntity> getAccounts() {
- return accounts;
- }
- public void setAccounts(Set<AccountEntity> accounts) {
- this.accounts = accounts;
- }
- }
Account实体
- package hibernate.test.oneToMany.foreignKeyAsso;
- import java.io.Serializable;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.ManyToOne;
- import javax.persistence.Table;
- import javax.persistence.UniqueConstraint;
- @Entity(name = "ForeignKeyAssoAccountEntity")
- @Table(name = "ACCOUNT", uniqueConstraints = {
- @UniqueConstraint(columnNames = "ID")})
- public class AccountEntity implements Serializable
- {
- private static final long serialVersionUID = -6790693372846798580L;
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "ID", unique = true, nullable = false)
- private Integer accountId;
- @Column(name = "ACC_NUMBER", unique = true, nullable = false, length = 100)
- private String accountNumber;
- @ManyToOne
- private EmployeeEntity employee;
- public Integer getAccountId() {
- return accountId;
- }
- public void setAccountId(Integer accountId) {
- this.accountId = accountId;
- }
- public String getAccountNumber() {
- return accountNumber;
- }
- public void setAccountNumber(String accountNumber) {
- this.accountNumber = accountNumber;
- }
- public EmployeeEntity getEmployee() {
- return employee;
- }
- public void setEmployee(EmployeeEntity employee) {
- this.employee = employee;
- }
- }
测试代码
- package hibernate.test.oneToMany;
- import hibernate.test.HibernateUtil;
- import hibernate.test.oneToMany.foreignKeyAsso.AccountEntity;
- import hibernate.test.oneToMany.foreignKeyAsso.EmployeeEntity;
- import java.util.HashSet;
- import java.util.Set;
- import org.hibernate.Session;
- public class TestForeignKeyAssociation
- {
- public static void main(String[] args)
- {
- Session session = HibernateUtil.getSessionFactory().openSession();
- session.beginTransaction();
- AccountEntity account1 = new AccountEntity();
- account1.setAccountNumber("Account detail 1");
- AccountEntity account2 = new AccountEntity();
- account2.setAccountNumber("Account detail 2");
- AccountEntity account3 = new AccountEntity();
- account3.setAccountNumber("Account detail 3");
- //Add new Employee object
- EmployeeEntity firstEmployee = new EmployeeEntity();
- firstEmployee.setEmail("[email protected]");
- firstEmployee.setFirstName("demo-one");
- firstEmployee.setLastName("user-one");
- EmployeeEntity secondEmployee = new EmployeeEntity();
- secondEmployee.setEmail("[email protected]");
- secondEmployee.setFirstName("demo-two");
- secondEmployee.setLastName("user-two");
- Set<AccountEntity> accountsOfFirstEmployee = new HashSet<AccountEntity>();
- accountsOfFirstEmployee.add(account1);
- accountsOfFirstEmployee.add(account2);
- Set<AccountEntity> accountsOfSecondEmployee = new HashSet<AccountEntity>();
- accountsOfSecondEmployee.add(account3);
- firstEmployee.setAccounts(accountsOfFirstEmployee);
- secondEmployee.setAccounts(accountsOfSecondEmployee);
- //Save Employee
- session.save(firstEmployee);
- session.save(secondEmployee);
- session.getTransaction().commit();
- HibernateUtil.shutdown();
- }
- }
- Output:
- Hibernate: insert into Employee (EMAIL, FIRST_NAME, LAST_NAME) values (?, ?, ?)
- Hibernate: insert into ACCOUNT (ACC_NUMBER, employee_ID) values (?, ?)
- Hibernate: insert into ACCOUNT (ACC_NUMBER, employee_ID) values (?, ?)
- Hibernate: insert into Employee (EMAIL, FIRST_NAME, LAST_NAME) values (?, ?, ?)
- Hibernate: insert into ACCOUNT (ACC_NUMBER, employee_ID) values (?, ?)
- Hibernate: update ACCOUNT set EMPLOYEE_ID=? where ID=?
- Hibernate: update ACCOUNT set EMPLOYEE_ID=? where ID=?
- Hibernate: update ACCOUNT set EMPLOYEE_ID=? where ID=?
使用关联表
这种方式使用关联表存储两个实体间的关系@JoinTable注解是用来建立这种关系的,先来看一下数据库模式
EMPLOYEE实体
- package hibernate.test.oneToMany.joinTable;
- import java.io.Serializable;
- import java.util.Set;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.JoinTable;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- import javax.persistence.UniqueConstraint;
- @Entity(name = "JoinTableEmployeeEntity")
- @Table(name = "Employee", uniqueConstraints = {
- @UniqueConstraint(columnNames = "ID"),
- @UniqueConstraint(columnNames = "EMAIL") })
- public class EmployeeEntity implements Serializable
- {
- private static final long serialVersionUID = -1798070786993154676L;
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "ID", unique = true, nullable = false)
- private Integer employeeId;
- @Column(name = "EMAIL", unique = true, nullable = false, length = 100)
- private String email;
- @Column(name = "FIRST_NAME", unique = false, nullable = false, length = 100)
- private String firstName;
- @Column(name = "LAST_NAME", unique = false, nullable = false, length = 100)
- private String lastName;
- @OneToMany(cascade=CascadeType.ALL)
- @JoinTable(name="EMPLOYEE_ACCOUNT", joinColumns={@JoinColumn(name="EMPLOYEE_ID", referencedColumnName="ID")}
- , inverseJoinColumns={@JoinColumn(name="ACCOUNT_ID", referencedColumnName="ID")})
- private Set<AccountEntity> accounts;
- public Integer getEmployeeId() {
- return employeeId;
- }
- public void setEmployeeId(Integer employeeId) {
- this.employeeId = employeeId;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public Set<AccountEntity> getAccounts() {
- return accounts;
- }
- public void setAccounts(Set<AccountEntity> accounts) {
- this.accounts = accounts;
- }
- }
Account实体
- package hibernate.test.oneToMany.joinTable;
- import java.io.Serializable;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.Table;
- import javax.persistence.UniqueConstraint;
- @Entity(name = "JoinTableAccountEntity")
- @Table(name = "ACCOUNT", uniqueConstraints = {
- @UniqueConstraint(columnNames = "ID")})
- public class AccountEntity implements Serializable
- {
- private static final long serialVersionUID = -6790693372846798580L;
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "ID", unique = true, nullable = false)
- private Integer accountId;
- @Column(name = "ACC_NUMBER", unique = true, nullable = false, length = 100)
- private String accountNumber;
- public Integer getAccountId() {
- return accountId;
- }
- public void setAccountId(Integer accountId) {
- this.accountId = accountId;
- }
- public String getAccountNumber() {
- return accountNumber;
- }
- public void setAccountNumber(String accountNumber) {
- this.accountNumber = accountNumber;
- }
- }
在配置文件中配置实体,我们已经有了两个在运行时的实体,我们必须在配置文件中增加他们。请注意只有一个集合实体可以在配置文件中配置,否则会有意外的情况发生
- < ?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">
- <hibernate-configuration>
- <session-factory>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetest</property>
- <property name="hibernate.connection.password">XXXXXX</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="show_sql">true</property>
- <property name="hbm2ddl.auto">create</property>
- <mapping clas="hibernate.test.oneToMany.foreignKeyAsso.AccountEntity"></mapping>
- <mapping clas="hibernate.test.oneToMany.foreignKeyAsso.EmployeeEntity"></mapping>
- </session-factory>
- </hibernate-configuration>
测试代码:
- package hibernate.test.oneToMany;
- import hibernate.test.HibernateUtil;
- import hibernate.test.oneToMany.joinTable.AccountEntity;
- import hibernate.test.oneToMany.joinTable.EmployeeEntity;
- import java.util.HashSet;
- import java.util.Set;
- import org.hibernate.Session;
- public class TestJoinTable
- {
- public static void main(String[] args)
- {
- Session session = HibernateUtil.getSessionFactory().openSession();
- session.beginTransaction();
- AccountEntity account1 = new AccountEntity();
- account1.setAccountNumber("123-345-65454");
- AccountEntity account2 = new AccountEntity();
- account2.setAccountNumber("123-345-6542222");
- //Add new Employee object
- EmployeeEntity emp = new EmployeeEntity();
- emp.setEmail("[email protected]");
- emp.setFirstName("demo");
- emp.setLastName("user");
- Set<AccountEntity> accounts = new HashSet<AccountEntity>();
- accounts.add(account1);
- accounts.add(account2);
- emp.setAccounts(accounts);
- //Save Employee
- session.save(emp);
- session.getTransaction().commit();
- HibernateUtil.shutdown();
- }
- }
- Output:
- Hibernate: insert into Employee (EMAIL, FIRST_NAME, LAST_NAME) values (?, ?, ?)
- Hibernate: insert into ACCOUNT (ACC_NUMBER) values (?)
- Hibernate: insert into ACCOUNT (ACC_NUMBER) values (?)
- Hibernate: insert into EMPLOYEE_ACCOUNT (EMPLOYEE_ID, ACCOUNT_ID) values (?, ?)
- Hibernate: insert into EMPLOYEE_ACCOUNT (EMPLOYEE_ID, ACCOUNT_ID) values (?, ?)
以上是关于使用注解的Hibernate one-to-many映射的主要内容,如果未能解决你的问题,请参考以下文章