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
Copy file name to clipboardExpand all lines: README.md
+56-14Lines changed: 56 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -98,7 +98,7 @@ You can find the sample code from: https://github.com/tdilber/spring-jpa-dynamic
98
98
<dependency>
99
99
<groupId>io.github.tdilber</groupId>
100
100
<artifactId>spring-jpa-dynamic-query</artifactId>
101
-
<version>0.5.0</version>
101
+
<version>0.6.0</version>
102
102
</dependency>
103
103
```
104
104
@@ -450,6 +450,8 @@ where authorizat4_.menu_icon like ?
450
450
Spring Data projections always boring. But this project projections are very simple.
451
451
There are two ways to use projections. I suggested using the second way. Because the second way is easier and more reusable.
452
452
453
+
**Note:** Record class is supported for projection. You can use record class for projection.
454
+
453
455
#### A- Manual Projection
454
456
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
455
457
select clause. You can also use the `Pair` class to give an alias to the field.
@@ -497,54 +499,94 @@ where authorizat4_.menu_icon like ?
497
499
498
500
_Note: you can find the example on demo github repository._
499
501
500
-
501
-
#### B- Auto Projection with Annotated Model
502
-
Model Annotations: `@JdqModel`, `@JdqField`, `@JdqIgnoreField`
502
+
#### B- Auto Projection with Annotated Model
503
+
Model Annotations: `@JdqModel`, `@JdqField`, `@JdqIgnoreField`, `@JdqSubModel`
503
504
504
505
We are discovering select clause if model has `@JdqModel` annotation AND select clause is empty.
505
-
Autofill Rules are Simple:
506
+
Autofill Rules are Simple:
506
507
- If field has `@JdqField` annotation, we are using this field name in the select clause.
507
508
- If field has not any annotation, we are using field name in the select clause.
508
509
- If field has `@JdqIgnoreField` annotation, we are ignoring this field in the select clause.
510
+
- If field has `@JdqSubModel` annotation, we are including the sub-model fields in the select clause.
509
511
510
512
**Usage of `@JdqField` annotation:**
511
513
512
514
`@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.
513
515
514
-
_Examples:_
516
+
**Usage of `@JdqSubModel` annotation:**
517
+
518
+
`@JdqSubModel` annotation is used to include fields from a nested model in the select clause. This allows for more complex projections involving nested objects.
519
+
520
+
There are 2 usage of `@JdqSubModel` annotation:
521
+
- If you want to use nested model fields without join support, Use `@JdqSubModel()` annotation without any parameter.
522
+
- If you want to use nested model fields with join support, Use `@JdqSubModel("joined_column_name")` annotation with joined column name parameter.
523
+
524
+
_Examples:_
515
525
516
526
```java
517
527
@JdqModel// This annotation is required for using projection with joined column
518
528
@Data
519
529
publicstaticclassUserJdqModel {
520
530
@JdqField("name") // This annotation is not required. But if you want to use different field name in the result, you can use this annotation.
521
531
privateString nameButDifferentFieldName;
522
-
@JdqField("user.name") // This annotation is required for using joined column in the projection
523
-
privateStringuserNameWithJoin;
532
+
@JdqField("team.name") // This annotation is required for using joined column in the projection
533
+
privateStringteamNameWithJoin;
524
534
525
535
privateInteger age; // This field is in the select clause. Because this field has not any annotation.
526
-
536
+
527
537
@JdqIgnoreField// This annotation is required for ignoring this field in the select clause.
528
538
privateString surname;
539
+
540
+
@JdqSubModel// This annotation is used to include fields from a nested model without join support
541
+
privateAddressJdqModel address;
542
+
543
+
@JdqSubModel("department") // This annotation is used to include fields from a nested model with join support
544
+
privateDepartmentJdqModel departmentJdqModel;
545
+
}
546
+
547
+
@JdqModel
548
+
@Data
549
+
publicstaticclassAddressJdqModel {
550
+
@JdqField("address.street")
551
+
privateString street;
552
+
@JdqField("address.city")
553
+
privateString city;
529
554
}
530
555
556
+
@JdqModel
557
+
public record DepartmentJdqModel(@JdqField("id") Long departmentId, @JdqFieldString name) {
558
+
559
+
}
560
+
531
561
// USAGE EXAMPLE
532
-
List<UserJdqModel> result =customerRepository.findAll(dynamicQuery, UserJdqModel.class);
562
+
List<UserJdqModel> result =userRepository.findAll(dynamicQuery, UserJdqModel.class);
0 commit comments