Skip to content

Commit

Permalink
fix: lazy initialiation of relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed Oct 25, 2023
1 parent 1b9fde0 commit 4b1fe34
Show file tree
Hide file tree
Showing 41 changed files with 924 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public class Message extends AbstractAuditingEntity<Message, Long> implements Se

@OrderBy("name ASC")
@JsonIgnoreProperties(value = { "message" }, allowSetters = true)
@OneToMany(mappedBy = "message", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "message", cascade = CascadeType.ALL, orphanRemoval = true)
private Collection<MessageHeader> headers = new ArrayList<>();

@ManyToOne(fetch = FetchType.EAGER)
@ManyToOne(fetch = FetchType.LAZY)
@JsonIgnoreProperties(value = {"scenarioParameters", "scenarioActions", "scenarioMessages"}, allowSetters = true)
private ScenarioExecution scenarioExecution;

Expand All @@ -93,26 +93,14 @@ public Direction getDirection() {
return Direction.fromId(direction);
}

public void setDirection(Direction direction) {
this.direction = direction.id;
}

public String getPayload() {
return payload;
}

public void setPayload(String payload) {
this.payload = payload;
}

public String getCitrusMessageId() {
return citrusMessageId;
}

public void setCitrusMessageId(String citrusMessageId) {
this.citrusMessageId = citrusMessageId;
}

public void addHeader(MessageHeader messageHeader) {
headers.add(messageHeader);
messageHeader.setMessage(this);
Expand Down Expand Up @@ -208,18 +196,23 @@ protected Message getEntity() {
return message;
}

public MessageBuilder messageId(Long messageId) {
message.messageId = messageId;
return this;
}

public MessageBuilder direction(Direction direction) {
message.setDirection(direction);
message.direction = direction.getId();
return this;
}

public MessageBuilder payload(String payload) {
message.setPayload(payload);
message.payload = payload;
return this;
}

public MessageBuilder citrusMessageId(String citrusMessageId) {
message.setCitrusMessageId(citrusMessageId);
message.citrusMessageId = citrusMessageId;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import jakarta.persistence.OrderBy;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Size;
import org.springframework.util.StringUtils;

import java.io.Serial;
import java.io.Serializable;
import java.time.Instant;
Expand All @@ -36,8 +38,6 @@
import java.util.Collection;
import java.util.List;

import org.springframework.util.StringUtils;

/**
* JPA entity for representing a scenario execution
*/
Expand Down Expand Up @@ -75,16 +75,16 @@ public class ScenarioExecution implements Serializable {

@OrderBy("name ASC")
@OneToMany(mappedBy = "scenarioExecution", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonIgnoreProperties(value = { "scenarioExecution" }, allowSetters = true)
@JsonIgnoreProperties(value = {"scenarioExecution"}, allowSetters = true)
private List<ScenarioParameter> scenarioParameters = new ArrayList<>();

@OrderBy("actionId ASC")
@OneToMany(mappedBy = "scenarioExecution", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ScenarioAction> scenarioActions = new ArrayList<>();

@OrderBy("messageId ASC")
@OneToMany(mappedBy = "scenarioExecution", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@JsonIgnoreProperties(value = { "headers", "scenarioExecution" }, allowSetters = true)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "scenarioExecution", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonIgnoreProperties(value = {"headers", "scenarioExecution"}, allowSetters = true)
private List<Message> scenarioMessages = new ArrayList<>();

public static ScenarioExecutionBuilder builder() {
Expand Down Expand Up @@ -133,7 +133,7 @@ public String getErrorMessage() {

public void setErrorMessage(String errorMessage) throws ErrorMessageTruncationException {
this.errorMessage = errorMessage;
if(StringUtils.hasLength(this.errorMessage)) {
if (StringUtils.hasLength(this.errorMessage)) {
try {
int size = getClass().getDeclaredField("errorMessage").getAnnotation(Column.class).length();
int inLength = this.errorMessage.length();
Expand Down Expand Up @@ -192,13 +192,13 @@ public void removeScenarioMessage(Message scenarioMessage) {
@Override
public String toString() {
return "ScenarioExecution{" +
"executionId='" + getExecutionId() + "'" +
", startDate='" + getStartDate() + "'" +
", endDate='" + getEndDate() + "'" +
", scenarioName='" + getScenarioName() + "'" +
", status='" + getStatus() + "'" +
", errorMessage='" + getErrorMessage() + "'" +
"}";
"executionId='" + getExecutionId() + "'" +
", startDate='" + getStartDate() + "'" +
", endDate='" + getEndDate() + "'" +
", scenarioName='" + getScenarioName() + "'" +
", status='" + getStatus() + "'" +
", errorMessage='" + getErrorMessage() + "'" +
"}";
}

public enum Status {
Expand Down Expand Up @@ -233,18 +233,19 @@ public static class ScenarioExecutionBuilder {

private final ScenarioExecution scenarioExecution = new ScenarioExecution();

private ScenarioExecutionBuilder(){
private ScenarioExecutionBuilder() {
// Static access through entity
}

public ScenarioExecution build(){
public ScenarioExecution build() {
return scenarioExecution;
}

public ScenarioExecutionBuilder startDate(Instant startDate) {
scenarioExecution.setStartDate(startDate);
return this;
}

public ScenarioExecutionBuilder endDate(Instant endDate) {
scenarioExecution.setEndDate(endDate);
return this;
Expand All @@ -261,7 +262,7 @@ public ScenarioExecutionBuilder status(Status status) {
}

public ScenarioExecutionBuilder errorMessage(String errorMessage) throws ErrorMessageTruncationException {
scenarioExecution.setErrorMessage(errorMessage);
scenarioExecution.setErrorMessage(errorMessage);
return this;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import jakarta.persistence.MapsId;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;

import java.io.Serial;
import java.io.Serializable;
Expand Down Expand Up @@ -63,6 +64,7 @@ public class TestParameter extends AbstractAuditingEntity<TestParameter, TestPar
@Column(name = "parameter_value", nullable = false, updatable = false)
private String value;

@NotNull
@ManyToOne
@MapsId("testResultId")
@JoinColumn(name = "test_result_id", nullable = false)
Expand Down Expand Up @@ -130,7 +132,9 @@ public int hashCode() {
public String toString() {
return "TestParameter{" +
"key='" + getKey() + "'" +
"value='" + getValue() + "'" +
", value='" + getValue() + "'" +
", createdDate='" + getCreatedDate() + "'" +
", lastModifiedDate='" + getLastModifiedDate() + "'" +
"}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.citrusframework.simulator.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand Down Expand Up @@ -80,7 +81,8 @@ public class TestResult extends AbstractAuditingEntity<TestResult, Long> impleme
/**
* Optional test parameters
*/
@OneToMany(fetch = FetchType.EAGER, mappedBy = "testResult")
@OneToMany(fetch = FetchType.LAZY, mappedBy = "testResult")
@JsonIgnoreProperties(value = { "testResult" }, allowSetters = true)
private final Set<TestParameter> testParameters = new HashSet<>();

/**
Expand Down Expand Up @@ -198,14 +200,15 @@ public int hashCode() {
@Override
public String toString() {
return "TestResult{" +
"id='" + getId() + "'" +
", createdDate='" + getCreatedDate() + "'" +
", status='" + getStatus() + "'" +
"id=" + getId() +
", status=" + getStatus() +
", testName='" + getTestName() + "'" +
", className='" + getClassName() + "'" +
", errorMessage='" + getErrorMessage() + "'" +
", failureStack='" + getFailureStack() + "'" +
", failureType='" + getFailureType() + "'" +
", createdDate='" + getCreatedDate() + "'" +
", lastModifiedDate='" + getLastModifiedDate() + "'" +
"}";
}

Expand Down Expand Up @@ -244,8 +247,8 @@ public TestResultBuilder id(Long id) {
return this;
}

public TestResultBuilder status(Integer status) {
testResult.status = status;
public TestResultBuilder status(Status status) {
testResult.status = status.id;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import org.citrusframework.simulator.model.MessageHeader;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
Expand All @@ -21,10 +22,6 @@ default Optional<MessageHeader> findOneWithEagerRelationships(Long id) {
return this.findOneWithToOneRelationships(id);
}

default List<MessageHeader> findAllWithEagerRelationships() {
return this.findAllWithToOneRelationships();
}

default Page<MessageHeader> findAllWithEagerRelationships(Pageable pageable) {
return this.findAllWithToOneRelationships(pageable);
}
Expand All @@ -35,9 +32,10 @@ default Page<MessageHeader> findAllWithEagerRelationships(Pageable pageable) {
)
Page<MessageHeader> findAllWithToOneRelationships(Pageable pageable);

@Query("select messageHeader from MessageHeader messageHeader left join fetch messageHeader.message")
List<MessageHeader> findAllWithToOneRelationships();
@Query("select messageHeader from MessageHeader messageHeader left join fetch messageHeader.message where messageHeader.headerId = :headerId")
Optional<MessageHeader> findOneWithToOneRelationships(@Param("headerId") Long headerId);

@Query("select messageHeader from MessageHeader messageHeader left join fetch messageHeader.message where messageHeader.id =:id")
Optional<MessageHeader> findOneWithToOneRelationships(@Param("id") Long id);
@Override
@EntityGraph(attributePaths = {"message"})
Page<MessageHeader> findAll(Specification<MessageHeader> spec, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import org.citrusframework.simulator.model.Message;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
Expand All @@ -22,10 +23,6 @@ default Optional<Message> findOneWithEagerRelationships(Long id) {
return this.findOneWithToOneRelationships(id);
}

default List<Message> findAllWithEagerRelationships() {
return this.findAllWithToOneRelationships();
}

default Page<Message> findAllWithEagerRelationships(Pageable pageable) {
return this.findAllWithToOneRelationships(pageable);
}
Expand All @@ -36,9 +33,10 @@ default Page<Message> findAllWithEagerRelationships(Pageable pageable) {
)
Page<Message> findAllWithToOneRelationships(Pageable pageable);

@Query("select message from Message message left join fetch message.scenarioExecution")
List<Message> findAllWithToOneRelationships();
@Query("select message from Message message left join fetch message.headers left join fetch message.scenarioExecution where message.messageId = :messageId")
Optional<Message> findOneWithToOneRelationships(@Param("messageId") Long messageId);

@Query("select message from Message message left join fetch message.scenarioExecution where message.id =:id")
Optional<Message> findOneWithToOneRelationships(@Param("id") Long id);
@Override
@EntityGraph(attributePaths = {"headers", "scenarioExecution"})
Page<Message> findAll(Specification<Message> spec, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

import org.citrusframework.simulator.model.TestResult;
import org.citrusframework.simulator.service.dto.TestResultByStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Optional;

/**
* Spring Data JPA repository for the {@link TestResult} entity.
*/
Expand All @@ -18,4 +24,16 @@ public interface TestResultRepository extends JpaRepository<TestResult, Long>, J
"sum(case when t.status = 2 then 1 else 0 end)) " +
"from TestResult t")
TestResultByStatus countByStatus();

@Override
@EntityGraph(attributePaths = {"testParameters"})
Page<TestResult> findAll(Pageable pageable);

@Override
@EntityGraph(attributePaths = {"testParameters"})
Page<TestResult> findAll(Specification<TestResult> spec, Pageable pageable);

@Override
@EntityGraph(attributePaths = {"testParameters"})
Optional<TestResult> findById(Long id);
}
Loading

0 comments on commit 4b1fe34

Please sign in to comment.