Skip to content

Commit

Permalink
Merge pull request #262 from xenit-eu/lazy-fetchtype
Browse files Browse the repository at this point in the history
Add lazy loading of one-to-one and many-to-one relations [ACC-1539]
  • Loading branch information
NielsCW authored Aug 19, 2024
2 parents 0f4b942 + 410568c commit 67a44f9
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 9 deletions.
1 change: 1 addition & 0 deletions contentgrid-spring-boot-starter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
implementation project(':contentgrid-spring-boot-autoconfigure')

runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.fasterxml.jackson.datatype:jackson-datatype-hibernate6'
runtimeOnly 'org.postgresql:postgresql'
runtimeOnly 'io.micrometer:micrometer-registry-prometheus'
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.contentgrid.spring.test.fixture.invoicing.repository.ShippingLabelRepository;
import com.contentgrid.spring.test.security.WithMockJwt;
import java.util.Set;
import java.util.UUID;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -47,7 +46,7 @@ class HalLinkTitlesAndFormPromptsTest {
void setup() {
customer = customerRepository.save(new Customer("Abc", "ABC"));
invoice = invoiceRepository.save(new Invoice("12345678", true, true, customer, Set.of()));
shippingLabel = shippingLabelRepository.save(new ShippingLabel(UUID.randomUUID(), "here", "there", null, null));
shippingLabel = shippingLabelRepository.save(new ShippingLabel("here", "there"));
}

@AfterEach
Expand Down Expand Up @@ -151,6 +150,10 @@ void contentFieldCamelCasedInCreateForm() throws Exception {
type: "text",
required: true
},
{
name: "parent",
type: "url"
},
{
name: "barcodePicture",
type: "file"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.contentgrid.spring.test.fixture.invoicing.model.Order;
import com.contentgrid.spring.test.fixture.invoicing.model.PromotionCampaign;
import com.contentgrid.spring.test.fixture.invoicing.model.ShippingAddress;
import com.contentgrid.spring.test.fixture.invoicing.model.ShippingLabel;
import com.contentgrid.spring.test.fixture.invoicing.repository.CustomerRepository;
import com.contentgrid.spring.test.fixture.invoicing.repository.InvoiceRepository;
import com.contentgrid.spring.test.fixture.invoicing.repository.OrderRepository;
Expand Down Expand Up @@ -296,6 +297,27 @@ void listRefunds_returns_http200_ok() throws Exception {
.andExpect(jsonPath("$._links.self.href").value("http://localhost/refunds?page=0&size=20"))
.andExpect(jsonPath("$._links.curies").value(curies()));
}

@Test
void listShippingLabels_withLabelsInLoop_http200_ok() throws Exception {
// set up the loop of shipping-labels
var label1 = shippingLabels.save(new ShippingLabel("a", "b"));
label1 = shippingLabels.save(label1);

var label2 = shippingLabels.save(new ShippingLabel("b", "a"));
label2 = shippingLabels.save(label2);

// Add relations
label1.setParent(label2);
label1 = shippingLabels.save(label1);

label2.setParent(label1);
shippingLabels.save(label2);

mockMvc.perform(get("/shipping-labels")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import jakarta.persistence.AttributeOverride;
import jakarta.persistence.Embedded;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.OneToOne;
import jakarta.validation.constraints.NotNull;
Expand Down Expand Up @@ -120,7 +121,7 @@ public void setAuditMetadata(AuditMetadata auditMetadata) {
@JsonProperty("attachment_filename")
private String attachmentFilename;

@ManyToOne(optional = false)
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "counterparty", nullable = false)
@org.springframework.data.rest.core.annotation.RestResource(rel = "d:counterparty")
@CollectionFilterParam(predicate = EntityId.class, documented = false)
Expand All @@ -134,7 +135,7 @@ public void setAuditMetadata(AuditMetadata auditMetadata) {
@org.springframework.data.rest.core.annotation.RestResource(rel = "d:orders")
private Set<Order> orders = new HashSet<>();

@OneToOne(mappedBy = "invoice")
@OneToOne(mappedBy = "invoice", fetch = FetchType.LAZY)
@org.springframework.data.rest.core.annotation.RestResource(rel = "d:refund")
@Null(groups = OnEntityDelete.class)
// Refund#invoice is required, this constraint ensures that there is no refund linked when deleting the invoice
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.annotation.JsonProperty.Access;
import jakarta.persistence.Column;
import jakarta.persistence.Version;
import jakarta.persistence.FetchType;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -38,7 +39,7 @@ public class Order {
@Version
private Long version = 0L;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "customer")
@CollectionFilterParam
@CollectionFilterParam(value = "customer._id", predicate = EntityId.class, documented = false)
Expand All @@ -60,7 +61,7 @@ public class Order {
@CollectionFilterParam(value = "manual_promos._id", predicate = EntityId.class, documented = false)
private Set<PromotionCampaign> manualPromos = new HashSet<>();

@OneToOne
@OneToOne(fetch = FetchType.LAZY)
@JsonProperty("shipping_address")
@CollectionFilterParam("shipping_address")
@CollectionFilterParam(value = "shipping_address._id", predicate = EntityId.class, documented = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.contentgrid.spring.querydsl.predicate.EntityId;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty.Access;
import jakarta.persistence.FetchType;
import jakarta.validation.constraints.NotNull;
import java.util.UUID;
import jakarta.persistence.Entity;
Expand All @@ -25,7 +26,7 @@ public class Refund {
@JsonProperty(access = Access.READ_ONLY)
private UUID id;

@OneToOne(optional = false)
@OneToOne(optional = false, fetch = FetchType.LAZY)
@CollectionFilterParam
@CollectionFilterParam(predicate = EntityId.class, documented = false)
@org.springframework.data.rest.core.annotation.RestResource(rel = "d:invoice")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.contentgrid.spring.test.fixture.invoicing.model;

import com.contentgrid.spring.querydsl.annotation.CollectionFilterParam;
import com.contentgrid.spring.querydsl.predicate.EntityId;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty.Access;
import jakarta.persistence.FetchType;
import java.util.UUID;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
Expand Down Expand Up @@ -31,7 +31,7 @@ public class ShippingAddress {
private String zip;
private String city;

@OneToOne(mappedBy = "shippingAddress")
@OneToOne(mappedBy = "shippingAddress", fetch = FetchType.LAZY)
@RestResource(rel = "d:order")
private Order order;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
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.validation.constraints.NotNull;
import java.util.UUID;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -42,6 +45,12 @@ public class ShippingLabel {
@NotNull
private String to;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent")
@CollectionFilterParam
@org.springframework.data.rest.core.annotation.RestResource(rel = "d:parent")
private ShippingLabel parent;

@Embedded
@AttributeOverride(name = "id", column = @Column(name = "barcode_picture__id"))
@AttributeOverride(name = "length", column = @Column(name = "barcode_picture__length"))
Expand All @@ -61,4 +70,9 @@ public class ShippingLabel {
@RestResource(linkRel = "d:package", path = "package")
@JsonProperty("package")
private Content _package;

public ShippingLabel(String from, String to) {
this.from = from;
this.to = to;
}
}

0 comments on commit 67a44f9

Please sign in to comment.