Table-Level Relationship

Let's look at our two tables again:

id name occupation salary age join_date
1 Ivanov Ivan Programmer 100000 25 2012-06-30
2 Petrov Petr Programmer 80000 23 2013-08-12
3 Ivanov Sergey Tester 40000 thirty 2014-01-01
4 Rabinovich Moisha Director 200000 35 2015-05-12
5 Kirienko Anastasia Office Manager 40000 25 2015-10-10
6 Vaska Cat 1000 3 2018-11-11

employee table:

This table has the following columns:

  • id INT
  • name VARCHAR
  • occupation VARCHA
  • salary INT
  • age INT
  • join_date DATE

And this is how the task table , which contains tasks for employees, looks like:

id emploee_id name deadline
1 1 Fix a bug on the frontend 2022-06-01
2 2 Fix a bug on the backend 2022-06-15
3 5 Buy coffee 2022-07-01
4 5 Buy coffee 2022-08-01
5 5 Buy coffee 2022-09-01
6 (NULL) Clean up the office (NULL)
7 4 Enjoy life (NULL)
8 6 Enjoy life (NULL)

This table has only 4 columns:

  • id is the unique number of the task (and rows in the table).
  • employee_id - ID of the employee from the employee table that the task is assigned to.
  • name - the name and description of the task.
  • deadline - the time by which the task must be completed.

A single entry in the employee table can be referenced by many rows in the task table. Such a table-level relationship is called one-to -many.

Relationship to the Java class level

And our classes, the Employee class :

@Entity
@Table(name="user")
class Employee {
   @Column(name="id")
   public Integer id;

   @Column(name="name")
   public String name;

   @Column(name="occupation")
   public String occupation;

   @Column(name="salary")
   public Integer salary;

   @Column(name="join_date")
   public Date join;
}

And the EmployeeTask class in its original form:

@Entity
@Table(name="task")
class EmployeeTask {
   @Column(name="id")
   public Integer id;

   @Column(name="name")
   public String description;

   @Column(name="employee_id")
   public Integer employeeId;

   @Column(name="deadline")
   public Date deadline;
}

@OneToMany annotation

We can arrange communication between Entity classes in a different way.

Remember the @ElementCollection annotation that we used to create a collection of child objects in the parent class? Something similar can be done with the @OneToMany annotation . Only this time, the Employee class will be changed :

@Entity
@Table(name="user")
class Employee {
   @Column(name="id")
   public Integer id;

   @OneToMany(cascade = CascadeType.ALL)
   @JoinColumn(name = "employee_id")
   private Set<EmployeeTask> tasks = new HashSet<EmployeeTask>();
}

Using the @OneToMany annotation , we indicated that the objectemployeecan store many EmployeeTask objects . Also, using the @JoinColumn annotation , we indicated in which column of the task table the object id is storedemployee.

However, the EmployeeTask class usually does not contain a field that refers to the employee_id column. Example:

@Entity
@Table(name="task")
class EmployeeTask {
   @Column(name="id")
   public Integer id;

   @Column(name="name")
   public String description;

   @Column(name="deadline")
   public Date deadline;
}

The employee_id field is considered a service field and its value is controlled by Hibernate.

Request examples

If you want to add some task to some worker, then you need to write code like this:

EmployeeTask task1 = new EmployeeTask();
task1.description = "Do Something Important";
session.persist(task1);

EmployeeTask task2 = new EmployeeTask();
task2.description = "Nothing to do";
session.persist(task2);
session.flush();

Employee director = session.find(Employee.class, 4);
director.tasks.add(task1);
director.tasks.add(task2);

session.update(director);
session.flush();

First, we create two EmployeeTask objects , save them to the database, and call the flush() method so that the INSERT operation is performed and the objects have IDs.

Then we find the director in the database, take the tasks field from him and add two tasks to him. Then we save the director to the database. After that, the value 4 will appear in the database for new tasks in the employee_id column - the id of the director in the employee table.

Important! The tables in the database are the same for the @ManyToOne and @OneToMany annotations . But the Java classes for these tables are different.

undefined
1
Task
Module 4. Working with databases, level 13, lesson 2
Locked
Author of the book or book of the author?
task1304