Skip to content

Commit 923c3d0

Browse files
committed
Improve JavaDoc comments
1 parent 229cfe0 commit 923c3d0

16 files changed

+299
-26
lines changed

doma-core/src/main/java/org/seasar/doma/AggregateStrategy.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,43 @@
2020
import java.lang.annotation.RetentionPolicy;
2121
import java.lang.annotation.Target;
2222

23-
/** Indicates a strategy for aggregating database records. */
23+
/**
24+
* Indicates a strategy for aggregating database records.
25+
*
26+
* <p>The element annotated with {@linkplain AggregateStrategy} must be an interface.
27+
*
28+
* <pre>
29+
* &#064;AggregateStrategy(root = Department.class, tableAlias = "d")
30+
* interface DepartmentStrategy {
31+
*
32+
* &#064;AssociationLinker(propertyPath = "employeeList", tableAlias = "e")
33+
* BiFunction&lt;Department, Employee, Department&gt; employeeList = (d, e) -> {
34+
* d.getEmployeeList().add(e);
35+
* e.setDepartment(d);
36+
* return d;
37+
* };
38+
*
39+
* &#064;AssociationLinker(propertyPath = "employeeList.address", tableAlias = "a")
40+
* BiFunction&lt;Employee, Address, Employee&gt; employeeListAddress = (e, a) -> {
41+
* e.setAddress(a);
42+
* return e;
43+
* };
44+
* }
45+
* </pre>
46+
*/
2447
@Target({ElementType.TYPE})
2548
@Retention(RetentionPolicy.RUNTIME)
2649
public @interface AggregateStrategy {
2750

51+
/**
52+
* Specifies the root entity class associated with the aggregation strategy.
53+
*
54+
* @return the root class
55+
*/
2856
Class<?> root();
2957

3058
/**
31-
* Specifies the table alias.
59+
* Specifies the root table alias.
3260
*
3361
* @return the alias name for a root table
3462
*/

doma-core/src/main/java/org/seasar/doma/Association.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@
2323
/**
2424
* Indicates an association between entities.
2525
*
26+
* <pre>
27+
* &#064;Entity
28+
* class Department {
29+
* &#064;Association
30+
* List&lt;Employee&gt; employeeList;
31+
* }
32+
*
33+
* &#064;Entity
34+
* class Employee {
35+
* &#064;Association
36+
* Department department;
37+
* }
38+
* </pre>
39+
*
2640
* <p>This annotation is applied to fields that represent a relationship between entities.
2741
*/
2842
@Target(ElementType.FIELD)

doma-core/src/main/java/org/seasar/doma/AssociationLinker.java

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,60 @@
2121
import java.util.function.BiFunction;
2222

2323
/**
24-
* Associates a field in an entity class with properties in a related object for the purpose of
25-
* creating object relationships when mapping between database tables and entities.
24+
* Indicates the execution of associations between entities.
2625
*
27-
* <p>This class can only be annotated on {@code public static final} fields of type {@link
28-
* BiFunction}.
26+
* <p>{@linkplain AssociationLinker} can be annotated on a field of {@link BiFunction}.
27+
*
28+
* <p>For the {@linkplain BiFunction}, specify the entity class represented by the {@link
29+
* #propertyPath()} as the second type parameter. For the first and third type parameters, specify
30+
* the source entity class associated with the entity class given in the second type parameter.
31+
*
32+
* <pre>
33+
* &#064;AggregateStrategy(root = Department.class, tableAlias = "d")
34+
* interface DepartmentStrategy {
35+
*
36+
* &#064;AssociationLinker(propertyPath = "employeeList", tableAlias = "e")
37+
* BiFunction&lt;Department, Employee, Department&gt; employeeList = (d, e) -> {
38+
* d.getEmployeeList().add(e);
39+
* e.setDepartment(d);
40+
* return d;
41+
* };
42+
*
43+
* &#064;AssociationLinker(propertyPath = "employeeList.address", tableAlias = "a")
44+
* BiFunction&lt;Employee, Address, Employee&gt; employeeListAddress = (e, a) -> {
45+
* e.setAddress(a);
46+
* return e;
47+
* };
48+
* }
49+
* </pre>
2950
*/
3051
@Target(java.lang.annotation.ElementType.FIELD)
3152
@Retention(RetentionPolicy.RUNTIME)
3253
public @interface AssociationLinker {
3354
/**
34-
* Specifies the path to the property in the related object.
55+
* Specifies the property path from the root entity using dot notation.
56+
*
57+
* <p>For example, suppose the root {@code Department} class has a property {@code List<Employee>
58+
* employeeList}, and {@code Employee} has a property {@code Address address}. In this case, the
59+
* property path representing {@code address} would be {@code employeeList.address}.
3560
*
3661
* @return the property path as a string
3762
*/
3863
String propertyPath();
3964

4065
/**
41-
* Defines the alias for the database table that is linked to the field.
66+
* Specifies the table alias for the entity class represented by the {@link #propertyPath()}.
67+
*
68+
* <p>For example, consider the following SQL:
69+
*
70+
* <pre>
71+
* SELECT * FROM DEPARTMENT d INNER JOIN EMPLOYEE e ON d.DEPARTMENT_ID = e.DEPARTMENT_ID
72+
* </pre>
73+
*
74+
* In this case, the {@code Employee} class corresponds to the {@code EMPLOYEE} table, and its
75+
* table alias is {@code e}.
76+
*
77+
* <p>The table alias concatenated with an underscore serves as the prefix for column aliases.
4278
*
4379
* @return the table alias as a string
4480
*/

doma-core/src/main/java/org/seasar/doma/Select.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.lang.annotation.RetentionPolicy;
2121
import java.lang.annotation.Target;
2222
import java.sql.Statement;
23-
import java.util.function.BiFunction;
2423
import org.seasar.doma.jdbc.Config;
2524
import org.seasar.doma.jdbc.JdbcException;
2625
import org.seasar.doma.jdbc.NoResultException;
@@ -166,10 +165,9 @@
166165
SqlLogType sqlLog() default SqlLogType.FORMATTED;
167166

168167
/**
169-
* Specifies a strategy class used for aggregation operations.
168+
* Defines how an aggregate is constructed.
170169
*
171-
* <p>The class specified here must contain at least one field of type {@link BiFunction}
172-
* annotated with {@link AssociationLinker}.
170+
* <p>Specifies the interface annotated with {@link AggregateStrategy}.
173171
*
174172
* @return the class representing the aggregation strategy, or {@code Void.class} if not specified
175173
*/

doma-core/src/main/java/org/seasar/doma/internal/jdbc/command/MappingSupport.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
import org.seasar.doma.jdbc.query.Query;
4141
import org.seasar.doma.wrapper.Wrapper;
4242

43+
/**
44+
* Provides support for handling and mapping database result sets to entity properties. It ensures
45+
* proper alignment between query results and entity fields, including handling unknown columns,
46+
* duplicate columns, and unmapped entity properties.
47+
*/
4348
public class MappingSupport {
4449

4550
private final EntityType<?> entityType;
@@ -61,6 +66,20 @@ public MappingSupport(
6166
this.duplicateColumnHandler = Objects.requireNonNull(duplicateColumnHandler);
6267
}
6368

69+
/**
70+
* Creates a mapping of result set column indices to their corresponding property types. This
71+
* method processes the column metadata and maps each column index to a {@code PropType}, based on
72+
* the provided column name to property type mapping.
73+
*
74+
* <p>Handles duplicate column names and unknown columns via the respective handlers. Throws an
75+
* exception if result mapping is enforced and there are unmapped properties.
76+
*
77+
* @param resultSetMeta the metadata of the result set
78+
* @param columnNameMap a map of lower-case column names to their corresponding property types
79+
* @return a map where keys are result set column indices and values are the corresponding
80+
* property types
81+
* @throws SQLException if a database access error occurs
82+
*/
6483
public Map<Integer, MappingSupport.PropType> createIndexMap(
6584
ResultSetMetaData resultSetMeta, Map<String, PropType> columnNameMap) throws SQLException {
6685
HashMap<Integer, PropType> indexMap = new HashMap<>();
@@ -91,6 +110,14 @@ public Map<Integer, MappingSupport.PropType> createIndexMap(
91110
return indexMap;
92111
}
93112

113+
/**
114+
* Throws a {@link ResultMappingException} when there are unmapped properties during result
115+
* mapping. This method gathers information about the unmapped properties, expected column names,
116+
* and relevant SQL details to construct and throw the exception.
117+
*
118+
* @param unmappedPropertySet the set of property types that could not be mapped to result set
119+
* columns
120+
*/
94121
private void throwResultMappingException(Set<PropType> unmappedPropertySet) {
95122
Naming naming = query.getConfig().getNaming();
96123
int size = unmappedPropertySet.size();
@@ -119,6 +146,17 @@ private void throwResultMappingException(Set<PropType> unmappedPropertySet) {
119146
sql.getSqlFilePath());
120147
}
121148

149+
/**
150+
* Represents a combination of an entity type, a property type, and the property path for mapping
151+
* between result set columns and properties in an entity.
152+
*
153+
* <p>This record encapsulates the basic metadata required for mapping a specific property of an
154+
* entity, including the property's name and its corresponding column in the database.
155+
*
156+
* @param entityType The metadata of the entity type that this property belongs to.
157+
* @param propertyType The metadata of the property type including its name and column details.
158+
* @param propertyPath The path representing the property within the entity structure.
159+
*/
122160
public record PropType(
123161
EntityType<?> entityType, EntityPropertyType<?, ?> propertyType, String propertyPath) {
124162
public String name() {
@@ -130,6 +168,16 @@ public String columnName(BiFunction<NamingType, String, String> namingFunction)
130168
}
131169
}
132170

171+
/**
172+
* Represents a property mapping for an entity during result set processing. This record
173+
* encapsulates metadata and value related to a specific property, providing utility methods for
174+
* accessing property-specific details.
175+
*
176+
* @param propType The type of the property containing metadata such as entity type, property
177+
* path, and property type.
178+
* @param property The underlying property object providing accessors and modifiers.
179+
* @param rawValue The raw value associated with this property, as retrieved from the data source.
180+
*/
133181
public record Prop(PropType propType, Property<Object, ?> property, Object rawValue) {
134182

135183
public EntityType<?> entityType() {

doma-core/src/main/java/org/seasar/doma/jdbc/aggregate/AbstractAggregateStrategyType.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ protected AbstractAggregateStrategyType(
3737
this.associationLinkerTypes = sortAssociationLinkerTypes(associationLinkerTypes);
3838
}
3939

40+
/**
41+
* Sorts a list of {@code AssociationLinkerType} objects in descending order based on their depth.
42+
*
43+
* @param associationLinkerTypes the list of {@code AssociationLinkerType} objects to be sorted
44+
* @return a sorted list of {@code AssociationLinkerType} objects in descending order of their
45+
* depth
46+
*/
4047
private static List<AssociationLinkerType<?, ?>> sortAssociationLinkerTypes(
4148
List<AssociationLinkerType<?, ?>> associationLinkerTypes) {
4249
Comparator<AssociationLinkerType<?, ?>> reversedComparator =

doma-core/src/main/java/org/seasar/doma/jdbc/aggregate/AggregateCommand.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.seasar.doma.jdbc.aggregate;
1717

18-
import java.util.Comparator;
1918
import java.util.LinkedHashMap;
2019
import java.util.List;
2120
import java.util.Map;
@@ -30,6 +29,14 @@
3029
import org.seasar.doma.jdbc.query.Query;
3130
import org.seasar.doma.jdbc.query.SelectQuery;
3231

32+
/**
33+
* The AggregateCommand class represents a command designed to execute an aggregate operation on a
34+
* stream of entities and return a result. This class implements the {@code Command} interface,
35+
* allowing for execution via a {@code execute} method and providing access to the underlying query.
36+
*
37+
* @param <RESULT> the result type of the aggregation
38+
* @param <ENTITY> the root entity type used within the aggregation
39+
*/
3340
public class AggregateCommand<RESULT, ENTITY> implements Command<RESULT> {
3441
private final SelectQuery query;
3542
private final EntityType<ENTITY> entityType;
@@ -47,14 +54,6 @@ public AggregateCommand(
4754
this.aggregateStrategyType = Objects.requireNonNull(aggregateStrategyType);
4855
}
4956

50-
private static List<AssociationLinkerType<?, ?>> sortAssociationLinkerTypes(
51-
List<AssociationLinkerType<?, ?>> associationLinkerTypes) {
52-
Comparator<AssociationLinkerType<?, ?>> reversedComparator =
53-
Comparator.<AssociationLinkerType<?, ?>>comparingInt(AssociationLinkerType::getDepth)
54-
.reversed();
55-
return associationLinkerTypes.stream().sorted(reversedComparator).toList();
56-
}
57-
5857
@Override
5958
public RESULT execute() {
6059
Map<LinkableEntityKey, Object> cache = new LinkedHashMap<>();
@@ -81,6 +80,21 @@ public RESULT execute() {
8180
return streamReducer.reduce(stream);
8281
}
8382

83+
/**
84+
* Establishes associations between source and target entities based on the provided combination
85+
* of linkage rules and updates the cache with newly associated entities. This method iterates
86+
* through association linker types and applies specific linking logic to pair source and target
87+
* entities according to their respective keys. The method avoids redundant associations by
88+
* checking existing combinations before linking.
89+
*
90+
* @param cache a map that stores entities indexed by their unique {@link LinkableEntityKey}. It
91+
* is updated during the association process with newly created entities.
92+
* @param combinations a set of previously established entity key pairs. This is used to ensure no
93+
* duplicate associations are created. New pairs are added to this set during the process.
94+
* @param associationCandidate a map containing potential source and target entities, where keys
95+
* are string identifiers corresponding to their roles (e.g., source or target) and values are
96+
* {@code LinkableEntity} objects.
97+
*/
8498
private void associate(
8599
Map<LinkableEntityKey, Object> cache,
86100
Combinations<LinkableEntityKey> combinations,

doma-core/src/main/java/org/seasar/doma/jdbc/aggregate/AggregateStrategyType.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import java.util.List;
1919
import org.seasar.doma.jdbc.entity.EntityType;
2020

21+
/**
22+
* Represents a strategy type for defining an aggregate structure that involves a root entity and
23+
* its associated entities.
24+
*/
2125
public interface AggregateStrategyType {
2226
EntityType<?> getRoot();
2327

doma-core/src/main/java/org/seasar/doma/jdbc/aggregate/AssociationIdentifier.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@
1818
import java.util.Objects;
1919
import org.seasar.doma.jdbc.entity.EntityType;
2020

21-
record AssociationIdentifier(String propertyPath, EntityType<?> entityType) {
22-
AssociationIdentifier {
21+
/**
22+
* Represents an identifier for an association in a data model. This identifier consists of a
23+
* property path and an entity type.
24+
*
25+
* @param propertyPath the path to the property, must not be {@code null}
26+
* @param entityType the entity type associated with the property, must not be {@code null}
27+
*/
28+
public record AssociationIdentifier(String propertyPath, EntityType<?> entityType) {
29+
public AssociationIdentifier {
2330
Objects.requireNonNull(propertyPath);
2431
Objects.requireNonNull(entityType);
2532
}

doma-core/src/main/java/org/seasar/doma/jdbc/aggregate/AssociationLinkerType.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
import java.util.stream.Collectors;
2222
import org.seasar.doma.jdbc.entity.EntityType;
2323

24+
/**
25+
* Represents an association linker that connects two entity types based on a property path.
26+
*
27+
* @param <S> the source entity type
28+
* @param <T> the target entity type
29+
*/
2430
public class AssociationLinkerType<S, T> {
2531

2632
private final String propertyPath;

0 commit comments

Comments
 (0)