You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -108,7 +108,7 @@ You can find the sample code from: https://github.com/tdilber/spring-jpa-dynamic
108
108
<dependency>
109
109
<groupId>io.github.tdilber</groupId>
110
110
<artifactId>spring-jpa-dynamic-query</artifactId>
111
-
<version>0.5.0</version>
111
+
<version>0.6.0</version>
112
112
</dependency>
113
113
```
114
114
@@ -460,6 +460,8 @@ where authorizat4_.menu_icon like ?
460
460
Spring Data projections always boring. But this project projections are very simple.
461
461
There are two ways to use projections. I suggested using the second way. Because the second way is easier and more reusable.
462
462
463
+
**Note:** Record class is supported for projection. You can use record class for projection.
464
+
463
465
#### A- Manual Projection
464
466
When you want to use specific fields in the result, you can add selected fields on select list on `DynamicQuery` object. You can add multiple fields to the
465
467
select clause. You can also use the `Pair` class to give an alias to the field.
@@ -507,54 +509,94 @@ where authorizat4_.menu_icon like ?
507
509
508
510
_Note: you can find the example on demo github repository._
509
511
510
-
511
-
#### B- Auto Projection with Annotated Model
512
-
Model Annotations: `@JdqModel`, `@JdqField`, `@JdqIgnoreField`
512
+
#### B- Auto Projection with Annotated Model
513
+
Model Annotations: `@JdqModel`, `@JdqField`, `@JdqIgnoreField`, `@JdqSubModel`
513
514
514
515
We are discovering select clause if model has `@JdqModel` annotation AND select clause is empty.
515
-
Autofill Rules are Simple:
516
+
Autofill Rules are Simple:
516
517
- If field has `@JdqField` annotation, we are using this field name in the select clause.
517
518
- If field has not any annotation, we are using field name in the select clause.
518
519
- If field has `@JdqIgnoreField` annotation, we are ignoring this field in the select clause.
520
+
- If field has `@JdqSubModel` annotation, we are including the sub-model fields in the select clause.
519
521
520
522
**Usage of `@JdqField` annotation:**
521
523
522
524
`@JdqField` annotation has a parameter. This parameter is a string. This string is a field name in the select clause. If you want to use different field name in the select clause, you can use this annotation. And also If you need to use joined column in the select clause, you can use this annotation.
523
525
524
-
_Examples:_
526
+
**Usage of `@JdqSubModel` annotation:**
527
+
528
+
`@JdqSubModel` annotation is used to include fields from a nested model in the select clause. This allows for more complex projections involving nested objects.
529
+
530
+
There are 2 usage of `@JdqSubModel` annotation:
531
+
- If you want to use nested model fields without join support, Use `@JdqSubModel()` annotation without any parameter.
532
+
- If you want to use nested model fields with join support, Use `@JdqSubModel("joined_column_name")` annotation with joined column name parameter.
533
+
534
+
_Examples:_
525
535
526
536
```java
527
537
@JdqModel// This annotation is required for using projection with joined column
528
538
@Data
529
539
publicstaticclassUserJdqModel {
530
540
@JdqField("name") // This annotation is not required. But if you want to use different field name in the result, you can use this annotation.
531
541
privateString nameButDifferentFieldName;
532
-
@JdqField("user.name") // This annotation is required for using joined column in the projection
533
-
privateStringuserNameWithJoin;
542
+
@JdqField("team.name") // This annotation is required for using joined column in the projection
543
+
privateStringteamNameWithJoin;
534
544
535
545
privateInteger age; // This field is in the select clause. Because this field has not any annotation.
536
-
546
+
537
547
@JdqIgnoreField// This annotation is required for ignoring this field in the select clause.
538
548
privateString surname;
549
+
550
+
@JdqSubModel// This annotation is used to include fields from a nested model without join support
551
+
privateAddressJdqModel address;
552
+
553
+
@JdqSubModel("department") // This annotation is used to include fields from a nested model with join support
554
+
privateDepartmentJdqModel departmentJdqModel;
555
+
}
556
+
557
+
@JdqModel
558
+
@Data
559
+
publicstaticclassAddressJdqModel {
560
+
@JdqField("address.street")
561
+
privateString street;
562
+
@JdqField("address.city")
563
+
privateString city;
539
564
}
540
565
566
+
@JdqModel
567
+
public record DepartmentJdqModel(@JdqField("id") Long departmentId, @JdqFieldString name) {
568
+
569
+
}
570
+
541
571
// USAGE EXAMPLE
542
-
List<UserJdqModel> result =customerRepository.findAll(dynamicQuery, UserJdqModel.class);
572
+
List<UserJdqModel> result =userRepository.findAll(dynamicQuery, UserJdqModel.class);
0 commit comments