Skip to content

Commit

Permalink
JPA Refactoring (4) (#2110)
Browse files Browse the repository at this point in the history
Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
  • Loading branch information
avgustinmm authored Dec 2, 2024
1 parent a9f3d14 commit 6f80038
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public class MgmtDistributionSetTagResourceTest extends AbstractManagementApiInt

@Test
@Description("Verfies that a paged result list of DS tags reflects the content on the repository side.")
@ExpectEvents({
@Expect(type = DistributionSetTagCreatedEvent.class, count = 2) })
@ExpectEvents({ @Expect(type = DistributionSetTagCreatedEvent.class, count = 2) })
public void getDistributionSetTags() throws Exception {
final List<DistributionSetTag> tags = testdataFactory.createDistributionSetTags(2);
final DistributionSetTag assigned = tags.get(0);
Expand Down Expand Up @@ -162,8 +161,7 @@ public void getDistributionSetTagsByDistributionSetIdAndTagDescription() throws

@Test
@Description("Verfies that a single result of a DS tag reflects the content on the repository side.")
@ExpectEvents({
@Expect(type = DistributionSetTagCreatedEvent.class, count = 2) })
@ExpectEvents({ @Expect(type = DistributionSetTagCreatedEvent.class, count = 2) })
public void getDistributionSetTag() throws Exception {
final List<DistributionSetTag> tags = testdataFactory.createDistributionSetTags(2);
final DistributionSetTag assigned = tags.get(0);
Expand All @@ -181,8 +179,7 @@ public void getDistributionSetTag() throws Exception {

@Test
@Description("Verifies that created DS tags are stored in the repository as send to the API.")
@ExpectEvents({
@Expect(type = DistributionSetTagCreatedEvent.class, count = 2) })
@ExpectEvents({ @Expect(type = DistributionSetTagCreatedEvent.class, count = 2) })
public void createDistributionSetTags() throws Exception {
final Tag tagOne = entityFactory.tag().create().colour("testcol1").description("its a test1").name("thetest1")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public class MgmtTargetTagResourceTest extends AbstractManagementApiIntegrationT

@Test
@Description("Verfies that a paged result list of target tags reflects the content on the repository side.")
@ExpectEvents({
@Expect(type = TargetTagCreatedEvent.class, count = 2) })
@ExpectEvents({ @Expect(type = TargetTagCreatedEvent.class, count = 2) })
public void getTargetTags() throws Exception {
final List<TargetTag> tags = testdataFactory.createTargetTags(2, "");
final TargetTag assigned = tags.get(0);
Expand Down Expand Up @@ -132,8 +131,7 @@ public void getTargetTagsFilteredByColor() throws Exception {

@Test
@Description("Verfies that a single result of a target tag reflects the content on the repository side.")
@ExpectEvents({
@Expect(type = TargetTagCreatedEvent.class, count = 2) })
@ExpectEvents({ @Expect(type = TargetTagCreatedEvent.class, count = 2) })
public void getTargetTag() throws Exception {
final List<TargetTag> tags = testdataFactory.createTargetTags(2, "");
final TargetTag assigned = tags.get(0);
Expand All @@ -152,8 +150,7 @@ public void getTargetTag() throws Exception {

@Test
@Description("Verifies that created target tags are stored in the repository as send to the API.")
@ExpectEvents({
@Expect(type = TargetTagCreatedEvent.class, count = 2) })
@ExpectEvents({ @Expect(type = TargetTagCreatedEvent.class, count = 2) })
public void createTargetTags() throws Exception {
final Tag tagOne = entityFactory.tag().create().colour("testcol1").description("its a test1").name("thetest1")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ private DistributionSetType assignSoftwareModuleTypes(
final Collection<JpaSoftwareModuleType> foundModules =
softwareModuleTypeRepository.findAllById(softwareModulesTypeIds);
if (foundModules.size() < softwareModulesTypeIds.size()) {
throw new EntityNotFoundException(SoftwareModuleType.class, softwareModulesTypeIds,
foundModules.stream().map(SoftwareModuleType::getId).toList());
throw new EntityNotFoundException(
SoftwareModuleType.class, softwareModulesTypeIds, foundModules.stream().map(SoftwareModuleType::getId).toList());
}

final JpaDistributionSetType type = findDistributionSetTypeAndThrowExceptionIfNotFound(dsTypeId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.persistence.annotations.Multitenant;
import org.eclipse.persistence.annotations.MultitenantType;
import org.eclipse.persistence.annotations.TenantDiscriminatorColumn;
import org.hibernate.annotations.TenantId;

/**
* Holder of the base attributes common to all tenant aware entities.
Expand All @@ -43,9 +44,11 @@ public abstract class AbstractJpaTenantAwareBaseEntity extends AbstractJpaBaseEn
@Serial
private static final long serialVersionUID = 1L;

@Column(name = "tenant", nullable = false, insertable = false, updatable = false, length = 40)
@Column(name = "tenant", nullable = false, insertable = false, updatable = false, length = 40) // eclipselink
// @Column(name = "tenant", nullable = false, updatable = false, length = 40) // hibernate
@Size(min = 1, max = 40)
@NotNull
@TenantId // Hibernate MultiTenant support
private String tenant;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;

import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Composite key for {@link DistributionSetTypeElement}.
*/
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Data
@Embeddable
public class DistributionSetTypeElementCompositeKey implements Serializable {

Expand All @@ -30,75 +36,8 @@ public class DistributionSetTypeElementCompositeKey implements Serializable {
@Column(name = "software_module_type", nullable = false, updatable = false)
private Long smType;

/**
* Default constructor.
*/
DistributionSetTypeElementCompositeKey() {
}

/**
* Constructor.
*
* @param dsType in the key
* @param smType in the key
*/
DistributionSetTypeElementCompositeKey(final JpaDistributionSetType dsType, final JpaSoftwareModuleType smType) {
this.dsType = dsType.getId();
this.smType = smType.getId();
}

public Long getDsType() {
return dsType;
}

public void setDsType(final Long dsType) {
this.dsType = dsType;
}

public Long getSmType() {
return smType;
}

public void setSmType(final Long smType) {
this.smType = smType;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dsType == null) ? 0 : dsType.hashCode());
result = prime * result + ((smType == null) ? 0 : smType.hashCode());
return result;
}

@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final DistributionSetTypeElementCompositeKey other = (DistributionSetTypeElementCompositeKey) obj;
if (dsType == null) {
if (other.dsType != null) {
return false;
}
} else if (!dsType.equals(other.dsType)) {
return false;
}
if (smType == null) {
if (other.smType != null) {
return false;
}
} else if (!smType.equals(other.smType)) {
return false;
}
return true;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.eclipse.hawkbit.repository.model.AutoConfirmationStatus;
import org.eclipse.hawkbit.repository.model.NamedEntity;
import org.eclipse.hawkbit.repository.model.Target;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,26 @@ public class JpaDistributionSet extends AbstractJpaNamedVersionedEntity implemen

@Setter
@ManyToOne(fetch = FetchType.LAZY, optional = false, targetEntity = JpaDistributionSetType.class)
@JoinColumn(name = "ds_id", nullable = false, updatable = false, foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_dstype_ds"))
@JoinColumn(
name = "ds_id", nullable = false, updatable = false,
foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_dstype_ds"))
@NotNull
private DistributionSetType type;

@ManyToMany(targetEntity = JpaSoftwareModule.class, fetch = FetchType.LAZY)
@ManyToMany(
targetEntity = JpaSoftwareModule.class, fetch = FetchType.LAZY,
cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinTable(
name = "sp_ds_module",
joinColumns = {
@JoinColumn(
name = "ds_id", nullable = false, insertable = false, updatable = false, foreignKey =
@ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_module_ds")) },
name = "ds_id", nullable = false, insertable = false, updatable = false,
foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_module_ds")) },
inverseJoinColumns = {
@JoinColumn(
name = "module_id", nullable = false, insertable = false, updatable = false,
foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_module_module")) })
private Set<SoftwareModule> modules;
private Set<SoftwareModule> modules = new HashSet<>();

@ManyToMany(targetEntity = JpaDistributionSetTag.class)
@JoinTable(
Expand All @@ -107,11 +111,12 @@ public class JpaDistributionSet extends AbstractJpaNamedVersionedEntity implemen
@JoinColumn(
name = "TAG", nullable = false, insertable = false, updatable = false,
foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_dstag_tag")) })
private Set<DistributionSetTag> tags;
private Set<DistributionSetTag> tags = new HashSet<>();

@ToString.Exclude
@OneToMany(mappedBy = "distributionSet", fetch = FetchType.LAZY, cascade = {
CascadeType.REMOVE }, targetEntity = JpaDistributionSetMetadata.class)
@OneToMany(mappedBy = "distributionSet", fetch = FetchType.LAZY,
cascade = { CascadeType.REMOVE },
targetEntity = JpaDistributionSetMetadata.class)
private List<DistributionSetMetadata> metadata;

@Column(name = "complete")
Expand All @@ -131,10 +136,8 @@ public class JpaDistributionSet extends AbstractJpaNamedVersionedEntity implemen
@Column(name = "required_migration_step")
private boolean requiredMigrationStep;

/**
* Parameterized constructor.
*/
public JpaDistributionSet(final String name, final String version, final String description,
public JpaDistributionSet(
final String name, final String version, final String description,
final DistributionSetType type, final Collection<SoftwareModule> moduleList,
final boolean requiredMigrationStep) {
super(name, version, description);
Expand All @@ -152,20 +155,13 @@ public JpaDistributionSet(final String name, final String version, final String
this.requiredMigrationStep = requiredMigrationStep;
}

/**
* Parameterized constructor.
*/
public JpaDistributionSet(final String name, final String version, final String description,
final DistributionSetType type, final Collection<SoftwareModule> moduleList) {
this(name, version, description, type, moduleList, false);
}

@Override
public Set<SoftwareModule> getModules() {
if (modules == null) {
return Collections.emptySet();
}

return Collections.unmodifiableSet(modules);
}

Expand All @@ -174,10 +170,6 @@ public void addModule(final SoftwareModule softwareModule) {
throw new LockedException(JpaDistributionSet.class, getId(), "ADD_SOFTWARE_MODULE");
}

if (modules == null) {
modules = new HashSet<>();
}

checkTypeCompatability(softwareModule);

final Optional<SoftwareModule> found = modules.stream()
Expand All @@ -187,8 +179,7 @@ public void addModule(final SoftwareModule softwareModule) {
return;
}

final long already = modules.stream()
.filter(module -> module.getType().getKey().equals(softwareModule.getType().getKey())).count();
final long already = modules.stream().filter(module -> module.getType().getKey().equals(softwareModule.getType().getKey())).count();

if (already >= softwareModule.getType().getMaxAssignments()) {
modules.stream().filter(module -> module.getType().getKey().equals(softwareModule.getType().getKey()))
Expand All @@ -211,10 +202,6 @@ public void removeModule(final SoftwareModule softwareModule) {
}

public Set<DistributionSetTag> getTags() {
if (tags == null) {
return Collections.emptySet();
}

return Collections.unmodifiableSet(tags);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.eclipse.hawkbit.repository.model.DistributionSet;
import org.eclipse.hawkbit.repository.model.DistributionSetMetadata;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ public void fireDeleteEvent() {
getTenant(), getId(), getClass(), EventPublisherHolder.getInstance().getApplicationId()));

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ public class JpaDistributionSetType extends AbstractJpaTypeEntity implements Dis
@Serial
private static final long serialVersionUID = 1L;

@OneToMany(mappedBy = "dsType", targetEntity = DistributionSetTypeElement.class, fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST,
CascadeType.REMOVE }, orphanRemoval = true)
@OneToMany(mappedBy = "dsType",
targetEntity = DistributionSetTypeElement.class,
fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE },
orphanRemoval = true)
private Set<DistributionSetTypeElement> elements = new HashSet<>();

@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,4 @@ public JpaSoftwareModuleMetadata(final String key, final SoftwareModule software
public SwMetadataCompositeKey getId() {
return new SwMetadataCompositeKey(softwareModule.getId(), getKey());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public interface DistributionSetRepository extends BaseEntityRepository<JpaDistr
* @param ids to search for
* @return list of {@link DistributionSet#getId()}
*/
@Query("select ac.distributionSet.id from JpaAction ac where ac.distributionSet.id in :ids")
@Query("SELECT ac.distributionSet.id FROM JpaAction ac WHERE ac.distributionSet.id IN :ids")
List<Long> findAssignedToTargetDistributionSetsById(@Param("ids") Collection<Long> ids);

/**
Expand All @@ -97,7 +97,7 @@ public interface DistributionSetRepository extends BaseEntityRepository<JpaDistr
* @param ids to search for
* @return list of {@link DistributionSet#getId()}
*/
@Query("select ra.distributionSet.id from JpaRollout ra where ra.distributionSet.id in :ids")
@Query("SELECT ra.distributionSet.id FROM JpaRollout ra WHERE ra.distributionSet.id IN :ids")
List<Long> findAssignedToRolloutDistributionSetsById(@Param("ids") Collection<Long> ids);

/**
Expand Down
Loading

0 comments on commit 6f80038

Please sign in to comment.