Hibernate JPA - RobertCzaja/it-offers-api GitHub Wiki

Persistence Mapping

  • orphanRemoval - (orphan - sierota) it's NOT correlated with ON DELETE CASCADE. It's ORM specific thing. When child entity is no longer referenced with parent entity - it's removed.

    • Example implementation: @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    • Error message: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: pl.api.itoffers.offer.domain.Offer.salary
  • mappedBy- MappedBy signals hibernate that the key for the relationship is on the other side. This means that although you link 2 tables together, only 1 of those tables has a foreign key constraint to the other one. MappedBy allows you to still link from the table not containing the constraint to the other table.

Cascade Persistence

@OneToMany(cascade = CascadeType.PERSIST) works but you can also use CascadeType.ALL.

Embedded

Embedded objects

@Embedded
private final Characteristics characteristics;
@Data
@Embeddable
@NoArgsConstructor(access = AccessLevel.PUBLIC, force = true)
@RequiredArgsConstructor
public class DeprecatedSalary {

    @Column(name = "salary_from")
    private final Double from;
    @Column(name = "salary_to")
    private final Double to;
    @Column(name = "salary_currency")
    private final String currency;
    @Column(name = "salary_employment_type")
    private final String employmentType;
}

Embeddable Id

@Data
@Embeddable
@NoArgsConstructor(access = AccessLevel.PUBLIC, force = true)
@RequiredArgsConstructor
public class SalaryId {
    private final UUID offerId;
    private final String currency;
}
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED, force = true)
@RequiredArgsConstructor
public class Salary {
    @EmbeddedId
    private final SalaryId id;
    @Embedded
    private final SalaryAmount amount;
    private final String employmentType;
    private final Boolean isOriginal;
}

Criteria Query