Browse Source

Hide `IdOnlyAggregateReference` class.

IdOnlyAggregateReference is an implementation detail of AggregateReference.

Closes #2106
pull/2114/head
Mark Paluch 4 months ago
parent
commit
e4136378dd
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 48
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/mapping/AggregateReference.java
  2. 45
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/mapping/IdOnlyAggregateReference.java
  3. 9
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/mapping/IdOnlyAggregateReferenceTest.java

48
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/mapping/AggregateReference.java

@ -15,11 +15,6 @@ @@ -15,11 +15,6 @@
*/
package org.springframework.data.jdbc.core.mapping;
import java.util.Objects;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* A reference to the aggregate root of a different aggregate.
*
@ -49,47 +44,4 @@ public interface AggregateReference<T, ID> { @@ -49,47 +44,4 @@ public interface AggregateReference<T, ID> {
*/
ID getId();
/**
* An {@link AggregateReference} that only holds the id of the referenced aggregate root. Note that there is no check
* that a matching aggregate for this id actually exists.
*
* @param <T>
* @param <ID>
*/
class IdOnlyAggregateReference<T, ID> implements AggregateReference<T, ID> {
private final ID id;
public IdOnlyAggregateReference(ID id) {
Assert.notNull(id, "Id must not be null");
this.id = id;
}
@Override
public ID getId() {
return id;
}
@Override
public boolean equals(@Nullable Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
IdOnlyAggregateReference<?, ?> that = (IdOnlyAggregateReference<?, ?>) o;
return id.equals(that.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "IdOnlyAggregateReference{" + "id=" + id + '}';
}
}
}

45
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/mapping/IdOnlyAggregateReference.java

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jdbc.core.mapping;
import org.springframework.util.Assert;
/**
* An {@link AggregateReference} that only holds the id of the referenced aggregate root. Note that there is no check
* that a matching aggregate for this id actually exists.
*
* @author Jens Schauder
* @author Mark Paluch
* @since 4.0
* @param <T>
* @param <ID>
*/
record IdOnlyAggregateReference<T, ID>(ID id) implements AggregateReference<T, ID> {
IdOnlyAggregateReference {
Assert.notNull(id, "Id must not be null");
}
@Override
public ID getId() {
return id();
}
@Override
public String toString() {
return "IdOnlyAggregateReference{" + "id=" + id + '}';
}
}

9
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/mapping/IdOnlyAggregateReferenceTest.java

@ -3,17 +3,16 @@ package org.springframework.data.jdbc.core.mapping; @@ -3,17 +3,16 @@ package org.springframework.data.jdbc.core.mapping;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.data.jdbc.core.mapping.AggregateReference.IdOnlyAggregateReference;
/**
* Unit tests for the {@link IdOnlyAggregateReference}.
*
* @author Myeonghyeon Lee
*/
public class IdOnlyAggregateReferenceTest {
class IdOnlyAggregateReferenceTest {
@Test // DATAJDBC-427
public void equals() {
void equals() {
AggregateReference<DummyEntity, String> reference1 = AggregateReference.to("1");
AggregateReference<DummyEntity, String> reference2 = AggregateReference.to("1");
@ -23,7 +22,7 @@ public class IdOnlyAggregateReferenceTest { @@ -23,7 +22,7 @@ public class IdOnlyAggregateReferenceTest {
}
@Test // DATAJDBC-427
public void equalsFalse() {
void equalsFalse() {
AggregateReference<DummyEntity, String> reference1 = AggregateReference.to("1");
AggregateReference<DummyEntity, String> reference2 = AggregateReference.to("2");
@ -33,7 +32,7 @@ public class IdOnlyAggregateReferenceTest { @@ -33,7 +32,7 @@ public class IdOnlyAggregateReferenceTest {
}
@Test // DATAJDBC-427
public void hashCodeTest() {
void hashCodeTest() {
AggregateReference<DummyEntity, String> reference1 = AggregateReference.to("1");
AggregateReference<DummyEntity, String> reference2 = AggregateReference.to("1");

Loading…
Cancel
Save