Skip to content

Commit

Permalink
tech: make all join annotations lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
maximeperrault authored and maximeperraultdev committed Jan 15, 2025
1 parent 9987b1d commit 0dfda03
Show file tree
Hide file tree
Showing 19 changed files with 493 additions and 567 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package fr.gouv.cacem.monitorenv.infrastructure.database.model

import fr.gouv.cacem.monitorenv.domain.entities.controlPlan.ControlPlanSubThemeEntity
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.Table
import jakarta.persistence.*
import org.hibernate.Hibernate
import org.hibernate.annotations.Cache
import org.hibernate.annotations.CacheConcurrencyStrategy
Expand All @@ -23,7 +16,7 @@ class ControlPlanSubThemeModel(
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false)
val id: Int,
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "theme_id")
val controlPlanTheme: ControlPlanThemeModel,
@Column(name = "subtheme") val subTheme: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package fr.gouv.cacem.monitorenv.infrastructure.database.model

import fr.gouv.cacem.monitorenv.domain.entities.controlPlan.ControlPlanTagEntity
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.Table
import jakarta.persistence.*
import org.hibernate.Hibernate
import org.hibernate.annotations.Cache
import org.hibernate.annotations.CacheConcurrencyStrategy
Expand All @@ -23,7 +16,7 @@ class ControlPlanTagModel(
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false)
val id: Int,
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "theme_id")
val controlPlanTheme: ControlPlanThemeModel,
@Column(name = "tag")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@ package fr.gouv.cacem.monitorenv.infrastructure.database.model
import com.fasterxml.jackson.annotation.JsonBackReference
import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.ControlUnitContactEntity
import fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit.dtos.FullControlUnitContactDTO
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.Table
import jakarta.persistence.*
import org.hibernate.annotations.CreationTimestamp
import org.hibernate.annotations.UpdateTimestamp
import java.time.Instant
Expand All @@ -22,7 +15,7 @@ data class ControlUnitContactModel(
@Column(name = "id", nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Int? = null,
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "control_unit_id", nullable = false)
@JsonBackReference
val controlUnit: ControlUnitModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,10 @@ import com.fasterxml.jackson.annotation.JsonManagedReference
import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.ControlUnitEntity
import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.LegacyControlUnitEntity
import fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit.dtos.FullControlUnitDTO
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.OneToMany
import jakarta.persistence.*
import jakarta.persistence.Table
import org.hibernate.annotations.*
import org.hibernate.annotations.Cache
import org.hibernate.annotations.CacheConcurrencyStrategy
import org.hibernate.annotations.CreationTimestamp
import org.hibernate.annotations.Fetch
import org.hibernate.annotations.FetchMode
import org.hibernate.annotations.UpdateTimestamp
import java.time.Instant

@Entity
Expand All @@ -33,7 +21,7 @@ data class ControlUnitModel(
@Column(name = "id", nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Int? = null,
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "administration_id", nullable = false)
@JsonBackReference
val administration: AdministrationModel,
Expand All @@ -42,12 +30,12 @@ data class ControlUnitModel(
@OneToMany(fetch = FetchType.LAZY, mappedBy = "controlUnit")
@JsonManagedReference
@Fetch(FetchMode.SUBSELECT)
val controlUnitContacts: List<ControlUnitContactModel>? = mutableListOf(),
val controlUnitContacts: MutableSet<ControlUnitContactModel>? = LinkedHashSet(),
@OneToMany(fetch = FetchType.LAZY, mappedBy = "controlUnit")
@JsonManagedReference
@Fetch(FetchMode.SUBSELECT)
val controlUnitResources: List<ControlUnitResourceModel>? = mutableListOf(),
@ManyToOne
val controlUnitResources: MutableSet<ControlUnitResourceModel>? = LinkedHashSet(),
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "department_area_insee_dep")
@JsonBackReference
val departmentArea: DepartmentAreaModel? = null,
Expand All @@ -73,8 +61,8 @@ data class ControlUnitModel(
controlUnit: ControlUnitEntity,
administrationModel: AdministrationModel,
departmentAreaModel: DepartmentAreaModel? = null,
controlUnitContactModels: List<ControlUnitContactModel>? = null,
controlUnitResourceModels: List<ControlUnitResourceModel>? = null,
controlUnitContactModels: MutableSet<ControlUnitContactModel>? = null,
controlUnitResourceModels: MutableSet<ControlUnitResourceModel>? = null,
): ControlUnitModel {
return ControlUnitModel(
id = controlUnit.id,
Expand Down Expand Up @@ -127,4 +115,27 @@ data class ControlUnitModel(
override fun toString(): String {
return this::class.simpleName + "(id = $id , administrationId = ${administration.id} , areaNote = $areaNote , departmentAreaInseeCode = ${departmentArea?.inseeCode} , isArchived = $isArchived, name = $name , termsNote = $termsNote)"
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as ControlUnitModel

if (id != other.id) return false
if (administration != other.administration) return false
if (areaNote != other.areaNote) return false
if (controlUnitContacts != other.controlUnitContacts) return false
if (controlUnitResources != other.controlUnitResources) return false
if (departmentArea != other.departmentArea) return false
if (isArchived != other.isArchived) return false
if (name != other.name) return false
if (termsNote != other.termsNote) return false
if (createdAtUtc != other.createdAtUtc) return false
if (updatedAtUtc != other.updatedAtUtc) return false

return true
}

override fun hashCode(): Int = javaClass.hashCode()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,7 @@ import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.ControlUnitResourceE
import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.ControlUnitResourceType
import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.LegacyControlUnitResourceEntity
import fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit.dtos.FullControlUnitResourceDTO
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.EnumType
import jakarta.persistence.Enumerated
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.Table
import jakarta.persistence.*
import org.hibernate.annotations.CreationTimestamp
import org.hibernate.annotations.JdbcType
import org.hibernate.annotations.UpdateTimestamp
Expand All @@ -28,7 +19,7 @@ data class ControlUnitResourceModel(
@Column(name = "id", nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Int? = null,
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "control_unit_id", nullable = false)
@JsonBackReference
val controlUnit: ControlUnitModel,
Expand All @@ -40,7 +31,7 @@ data class ControlUnitResourceModel(
val note: String? = null,
@Column(name = "photo")
val photo: ByteArray? = byteArrayOf(),
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "base_id", nullable = false)
@JsonBackReference
val station: StationModel,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
package fr.gouv.cacem.monitorenv.infrastructure.database.model

import fr.gouv.cacem.monitorenv.infrastructure.database.model.reportings.ReportingModel
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.OneToOne
import jakarta.persistence.Table
import java.util.UUID
import jakarta.persistence.*
import java.util.*

@Entity
@Table(name = "dashboard_datas")
Expand All @@ -23,19 +14,19 @@ data class DashboardDatasModel(
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "dashboard_id")
var dashboard: DashboardModel?,
@OneToOne
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "amp_cacem_id")
val amp: AMPModel?,
@OneToOne
@JoinColumn(name = "reportings_id")
val reportingModel: ReportingModel?,
@OneToOne
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "vigilance_area_id")
val vigilanceAreaModel: VigilanceAreaModel?,
@OneToOne
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "regulations_cacem_id")
val regulatoryAreaModel: RegulatoryAreaModel?,
@OneToOne
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "control_unit_id")
val controlUnitModel: ControlUnitModel?,
@Column(name = "insee_code")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
package fr.gouv.cacem.monitorenv.infrastructure.database.model

import jakarta.persistence.Column
import jakarta.persistence.Embeddable
import jakarta.persistence.EmbeddedId
import jakarta.persistence.Entity
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.MapsId
import jakarta.persistence.Table
import jakarta.persistence.*
import org.hibernate.Hibernate
import java.io.Serializable
import java.util.UUID
import java.util.*

@Entity
@Table(name = "env_actions_control_plan_sub_themes")
class EnvActionsControlPlanSubThemeModel(
@EmbeddedId
val id: EnvActionsSubThemePk,
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("envActionId")
@JoinColumn(name = "env_action_id")
val envAction: EnvActionModel? = null,
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("subthemeId")
@JoinColumn(name = "subtheme_id")
val controlPlanSubTheme: ControlPlanSubThemeModel? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
package fr.gouv.cacem.monitorenv.infrastructure.database.model

import jakarta.persistence.Column
import jakarta.persistence.Embeddable
import jakarta.persistence.EmbeddedId
import jakarta.persistence.Entity
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.MapsId
import jakarta.persistence.Table
import jakarta.persistence.*
import org.hibernate.Hibernate
import java.io.Serializable
import java.util.UUID
import java.util.*

@Entity
@Table(name = "env_actions_control_plan_tags")
class EnvActionsControlPlanTagModel(
@EmbeddedId
val id: EnvActionsTagPk,
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("envActionId")
@JoinColumn(name = "env_action_id")
val envAction: EnvActionModel? = null,
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("tagId")
@JoinColumn(name = "tag_id")
val controlPlanTag: ControlPlanTagModel? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
package fr.gouv.cacem.monitorenv.infrastructure.database.model

import jakarta.persistence.Column
import jakarta.persistence.Embeddable
import jakarta.persistence.EmbeddedId
import jakarta.persistence.Entity
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.MapsId
import jakarta.persistence.Table
import jakarta.persistence.*
import org.hibernate.Hibernate
import java.io.Serializable
import java.util.UUID
import java.util.*

@Entity
@Table(name = "env_actions_control_plan_themes")
class EnvActionsControlPlanThemeModel(
@EmbeddedId
val id: EnvActionsThemePk,
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("envActionId")
@JoinColumn(name = "env_action_id")
val envAction: EnvActionModel? = null,
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("themeId")
@JoinColumn(name = "theme_id")
val controlPlanTheme: ControlPlanThemeModel? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package fr.gouv.cacem.monitorenv.infrastructure.database.model

import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.LegacyControlUnitResourceEntity
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.Table
import jakarta.persistence.*

@Entity
@Table(name = "missions_control_resources")
Expand All @@ -17,10 +10,10 @@ data class MissionControlResourceModel(
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Int? = null,
@ManyToOne(optional = false)
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "mission_id")
val mission: MissionModel,
@ManyToOne(optional = false)
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "control_resource_id")
var resource: ControlUnitResourceModel,
) {
Expand All @@ -31,4 +24,24 @@ data class MissionControlResourceModel(
name = resource.name,
)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as MissionControlResourceModel

if (id != other.id) return false
if (mission != other.mission) return false
if (resource != other.resource) return false

return true
}

override fun hashCode(): Int {
var result = id ?: 0
result = 31 * result + mission.hashCode()
result = 31 * result + resource.hashCode()
return result
}
}
Loading

0 comments on commit 0dfda03

Please sign in to comment.