Sunday, 25 December 2022

Hibernate 6: @OneToMany: One to Many association

One to Many association links a parent entity with one or more child entities.

 

For example, an employee has one or more bank accounts.

 

There are two types of One to Many associations.

a.   Unidirectional one to many association: It do not have mirroring @ManyToOne (many to one) association on child entity.

b.   Bidirectional one to many association: It has mirroring @ManyToOne (many to one) association on child entity.

 

Unidirectional One to Many association

@Entity
@Table(name = "bank_account_details")
public class BankAccount {

	@Id
	private int id;

	private String accountNumber;

	private String branch;

	private String ifscCode;

	private String address;

	..........
	..........
}

@Entity
@Table(name = "employees")
public class Employee {
	@Id
	private int id;

	private String firstName;

	private String lastName;

	@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
	private Set<BankAccount> bankAccounts = new HashSet();

	...........
	...........
}

Above snippet generates below DDL.

    create table bank_account_details (
        id integer not null,
        accountNumber varchar(255),
        address varchar(255),
        branch varchar(255),
        ifscCode varchar(255),
        primary key (id)
    )

    create table employees (
        id integer not null,
        firstName varchar(255),
        lastName varchar(255),
        primary key (id)
    )
    
    create table employees_bank_account_details (
        Employee_id integer not null,
        bankAccounts_id integer not null,
        primary key (Employee_id, bankAccounts_id)
    )

Bidirectional one to many association example

@Entity
@Table(name = "bank_account_details")
public class BankAccount {

	@Id
	private int id;

	private String accountNumber;

	private String branch;

	private String ifscCode;

	private String address;

	@ManyToOne
	@JoinColumn(name = "employee_id", foreignKey = @ForeignKey(name = "bank_acc_emp_id_fk"))
	private Employee employee;

	...........
	...........
}

@Entity
@Table(name = "employees")
public class Employee {
	@Id
	private int id;

	private String firstName;

	private String lastName;

	@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY, mappedBy = "employee")
	private Set<BankAccount> bankAccounts = new HashSet<>();

	public void addBankAccount(BankAccount bankAccount) {
		bankAccounts.add(bankAccount);
		bankAccount.setEmployee(this);
	}

	public void removeBankAccount(BankAccount bankAccount) {
		bankAccounts.remove(bankAccount);
		bankAccount.setEmployee(this);
	}

	.............
	.............
}

Above snippet generates following DDL definition.

    create table bank_account_details (
       id integer not null,
        accountNumber varchar(255),
        address varchar(255),
        branch varchar(255),
        ifscCode varchar(255),
        employee_id integer,
        primary key (id)
    )

    create table employees (
       id integer not null,
        firstName varchar(255),
        lastName varchar(255),
        primary key (id)
    )
    
    alter table if exists bank_account_details 
       add constraint bank_acc_emp_id_fk 
       foreign key (employee_id) 
       references employees

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment