Relationships with Liquibase

1. One to One relationship with Liquibase:

package com.example.relationships_with_liquibase.models;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.Hibernate;

import java.util.Objects;

@Getter
@Setter
@Entity
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String identifier;
private String firstName;
private String lastName;
private String email;

@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "address_id")
private Address address;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
Employee employee = (Employee) o;
return id != null && Objects.equals(id, employee.id);
}

@Override
public int hashCode() {
return getClass().hashCode();
}

} 


package com.example.relationships_with_liquibase.models;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.Hibernate;

import java.util.Objects;

@Getter
@Setter
@Entity
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Address {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String streetName;
private String houseNumber;
private String zipCode;

@ToString.Exclude
@OneToOne(mappedBy = "address", orphanRemoval = true)
private Employee employee;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
Address address = (Address) o;
return id != null && Objects.equals(id, address.id);
}

@Override
public int hashCode() {
return getClass().hashCode();
}
}


databaseChangeLog:
- changeSet:
id: 20230826-01
author: parvin
changes:
- createTable:
tableName: employee
columns:
- column:
name: id
type: bigint
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: identifier
type: varchar(255)
- column:
name: first_name
type: varchar(255)
- column:
name: last_name
type: varchar(255)
- column:
name: email
type: varchar(255)
- column:
name: address_id
type: bigint


databaseChangeLog:
- changeSet:
id: 20230826-01
author: parvin
changes:
- createTable:
tableName: address
columns:
- column:
name: id
type: bigint
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: street_name
type: varchar(255)
- column:
name: house_number
type: varchar(255)
- column:
name: zip_code
type: varchar(255)


databaseChangeLog:
- changeSet:
id: 20230826-01
author: parvin
changes:
- addForeignKeyConstraint:
baseColumnNames: address_id
baseTableName: employee
constraintName: fk_employee_address
referencedColumnNames: id
referencedTableName: employee


 databaseChangeLog:
- includeAll:
- path: db/changelog/v1/
- includeAll:
- path: db/changelog/v2/

# file: db/changelog/v1/20230826-01-create-address-table.yaml
# - include:
# file: db/changelog/v1/20230826-01-create-employee-table.yaml
# - include:
# file: db/changelog/v1/20230826-01-add-foreign-key-constraint-between-employee-and-address.yaml
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/postgres
username: postgres
password: postgres
# JPA properties
jpa:
hibernate:
ddl-auto: validate # When you launch the application for the first time - switch "none" at "create"
show-sql: true
database: postgresql
database-platform: org.hibernate.dialect.PostgreSQLDialect
open-in-view: false
generate-ddl: true
liquibase:
enabled: true
change-log: classpath:/db/changelog/db.changelog-master.yaml



2. One to Many with Liquibase:


package com.example.relationships_with_liquibase.models;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.Hibernate;

import java.util.Objects;

@Getter
@Setter
@Entity
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String identifier;
private String firstName;
private String lastName;
private String email;

@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "address_id")
private Address address;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "department_id")
private Department department;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
Employee employee = (Employee) o;
return id != null && Objects.equals(id, employee.id);
}

@Override
public int hashCode() {
return getClass().hashCode();
}
}
package com.example.relationships_with_liquibase.models;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.Hibernate;

import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

@Getter
@Setter
@Entity
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Department {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;

@ToString.Exclude
@OneToMany(mappedBy = "department", orphanRemoval = true)
private Set<Employee> employees = new LinkedHashSet<>();

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
Department that = (Department) o;
return id != null && Objects.equals(id, that.id);
}

@Override
public int hashCode() {
return getClass().hashCode();
}
}


databaseChangeLog:
- changeSet:
id: 20230827-01
author: parvin
changes:
- createTable:
tableName: department
columns:
- column:
name: id
type: bigint
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: name
type: varchar(255)


databaseChangeLog:
- changeSet:
id: 20230827-01
author: parvin
changes:
- addForeignKeyConstraint:
baseColumnNames: department_id
baseTableName: employee
constraintName: fk_employee_department
referencedColumnNames: id
referencedTableName: department


3. Many to Many with Liquibase:


package com.example.relationships_with_liquibase.models;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.Hibernate;

import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

@Getter
@Setter
@Entity
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String identifier;
private String firstName;
private String lastName;
private String email;

@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "address_id")
private Address address;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "department_id")
private Department department;

@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
@JoinTable(name = "employee_missions",
joinColumns = @JoinColumn(name = "employee_id"),
inverseJoinColumns = @JoinColumn(name = "missions_id"))
private Set<Mission> missions = new LinkedHashSet<>();

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
Employee employee = (Employee) o;
return id != null && Objects.equals(id, employee.id);
}

@Override
public int hashCode() {
return getClass().hashCode();
}
}


package com.example.relationships_with_liquibase.models;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.Hibernate;

import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

@Getter
@Setter
@Entity
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Mission {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Integer duration;

@ToString.Exclude
@ManyToMany(mappedBy = "missions")
private Set<Employee> employees = new LinkedHashSet<>();

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
Mission mission = (Mission) o;
return id != null && Objects.equals(id, mission.id);
}

@Override
public int hashCode() {
return getClass().hashCode();
}
}


databaseChangeLog:
- changeSet:
id: 20230827-01
author: parvin
changes:
- createTable:
tableName: employee_missions
columns:
- column:
name: employee_id
type: bigint
constraints:
foreignKeyName: fk_employee_id
nullable: false
referencedTableName: employee
referencedColumnNames: id
- column:
name: missions_id
type: bigint
constraints:
foreignKeyName: fk_mission_id
nullable: false
referencedTableName: mission
referencedColumnNames: id
- addPrimaryKey:
tableName: employee_missions
constraintName: pk_employee_mission
columnNames: employee_id, missions_id















Комментарии

Популярные сообщения из этого блога

IoC:ApplicationContext, BeanFactory. Bean

Lesson1: JDK, JVM, JRE

Lesson_2: Operations in Java