|
16 | 16 |
|
17 | 17 | package com.mongodb.hibernate; |
18 | 18 |
|
| 19 | +import static com.mongodb.client.model.Aggregates.project; |
| 20 | +import static com.mongodb.client.model.Projections.include; |
| 21 | +import static com.mongodb.hibernate.BasicCrudIntegrationTests.Item.COLLECTION_NAME; |
19 | 22 | import static com.mongodb.hibernate.MongoTestAssertions.assertEq; |
| 23 | +import static com.mongodb.hibernate.internal.MongoConstants.ID_FIELD_NAME; |
20 | 24 | import static java.util.Arrays.asList; |
21 | 25 | import static org.assertj.core.api.Assertions.assertThat; |
22 | 26 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
|
29 | 33 | import com.mongodb.hibernate.internal.FeatureNotSupportedException; |
30 | 34 | import com.mongodb.hibernate.junit.InjectMongoCollection; |
31 | 35 | import com.mongodb.hibernate.junit.MongoExtension; |
| 36 | +import jakarta.persistence.ColumnResult; |
32 | 37 | import jakarta.persistence.ElementCollection; |
33 | 38 | import jakarta.persistence.Entity; |
34 | 39 | import jakarta.persistence.Id; |
| 40 | +import jakarta.persistence.SqlResultSetMapping; |
35 | 41 | import jakarta.persistence.Table; |
36 | 42 | import java.math.BigDecimal; |
37 | 43 | import java.sql.SQLFeatureNotSupportedException; |
38 | 44 | import java.util.Collection; |
39 | | -import java.util.LinkedHashSet; |
| 45 | +import java.util.HashSet; |
40 | 46 | import java.util.List; |
41 | 47 | import org.bson.BsonDocument; |
| 48 | +import org.bson.conversions.Bson; |
42 | 49 | import org.bson.types.ObjectId; |
43 | 50 | import org.hibernate.MappingException; |
44 | 51 | import org.hibernate.boot.MetadataSources; |
|
64 | 71 | @ServiceRegistry(settings = {@Setting(name = WRAPPER_ARRAY_HANDLING, value = "allow")}) |
65 | 72 | @ExtendWith(MongoExtension.class) |
66 | 73 | public class ArrayAndCollectionIntegrationTests implements SessionFactoryScopeAware { |
67 | | - private static final String COLLECTION_NAME = "items"; |
68 | | - |
69 | 74 | @InjectMongoCollection(COLLECTION_NAME) |
70 | 75 | private static MongoCollection<BsonDocument> mongoCollection; |
71 | 76 |
|
@@ -98,7 +103,7 @@ void testArrayAndCollectionValues() { |
98 | 103 | new StructAggregateEmbeddableIntegrationTests.Single(1), null |
99 | 104 | }, |
100 | 105 | asList('s', 't', null, 'r'), |
101 | | - asList(null, 5), |
| 106 | + new HashSet<>(asList(null, 5)), |
102 | 107 | asList(Long.MAX_VALUE, null, 6L), |
103 | 108 | asList(null, Double.MAX_VALUE), |
104 | 109 | asList(null, true), |
@@ -310,8 +315,7 @@ void testArrayAndCollectionValuesOfStructAggregateEmbeddablesHavingArraysAndColl |
310 | 315 | new StructAggregateEmbeddableIntegrationTests.Single(1) |
311 | 316 | }, |
312 | 317 | List.of('s', 't', 'r'), |
313 | | - // Hibernate ORM uses `LinkedHashSet`, forcing us to also use it, but messing up the order anyway |
314 | | - new LinkedHashSet<>(List.of(5)), |
| 318 | + new HashSet<>(List.of(5)), |
315 | 319 | List.of(Long.MAX_VALUE, 6L), |
316 | 320 | List.of(Double.MAX_VALUE), |
317 | 321 | List.of(true), |
@@ -486,11 +490,47 @@ private static void assertCollectionContainsExactly(String documentAsJsonObject) |
486 | 490 | assertThat(mongoCollection.find()).containsExactly(BsonDocument.parse(documentAsJsonObject)); |
487 | 491 | } |
488 | 492 |
|
| 493 | + /** @see BasicCrudIntegrationTests.Item */ |
489 | 494 | @Entity |
490 | 495 | @Table(name = COLLECTION_NAME) |
491 | | - static class ItemWithArrayAndCollectionValues { |
| 496 | + @SqlResultSetMapping( |
| 497 | + name = ItemWithArrayAndCollectionValues.MAPPING_FOR_ITEM, |
| 498 | + columns = { |
| 499 | + @ColumnResult(name = ID_FIELD_NAME), |
| 500 | + @ColumnResult(name = "bytes", type = byte[].class), |
| 501 | + @ColumnResult(name = "chars", type = char[].class), |
| 502 | + @ColumnResult(name = "ints", type = int[].class), |
| 503 | + @ColumnResult(name = "longs", type = long[].class), |
| 504 | + @ColumnResult(name = "doubles", type = double[].class), |
| 505 | + @ColumnResult(name = "booleans", type = boolean[].class), |
| 506 | + @ColumnResult(name = "boxedChars", type = Character[].class), |
| 507 | + @ColumnResult(name = "boxedInts", type = Integer[].class), |
| 508 | + @ColumnResult(name = "boxedLongs", type = Long[].class), |
| 509 | + @ColumnResult(name = "boxedDoubles", type = Double[].class), |
| 510 | + @ColumnResult(name = "boxedBooleans", type = Boolean[].class), |
| 511 | + @ColumnResult(name = "strings", type = String[].class), |
| 512 | + @ColumnResult(name = "bigDecimals", type = BigDecimal[].class), |
| 513 | + @ColumnResult(name = "objectIds", type = ObjectId[].class), |
| 514 | + @ColumnResult( |
| 515 | + name = "structAggregateEmbeddables", |
| 516 | + type = StructAggregateEmbeddableIntegrationTests.Single[].class), |
| 517 | + @ColumnResult(name = "charsCollection", type = Character[].class), |
| 518 | + @ColumnResult(name = "intsCollection", type = Integer[].class), |
| 519 | + @ColumnResult(name = "longsCollection", type = Long[].class), |
| 520 | + @ColumnResult(name = "doublesCollection", type = Double[].class), |
| 521 | + @ColumnResult(name = "booleansCollection", type = Boolean[].class), |
| 522 | + @ColumnResult(name = "stringsCollection", type = String[].class), |
| 523 | + @ColumnResult(name = "bigDecimalsCollection", type = BigDecimal[].class), |
| 524 | + @ColumnResult(name = "objectIdsCollection", type = ObjectId[].class), |
| 525 | + @ColumnResult( |
| 526 | + name = "structAggregateEmbeddablesCollection", |
| 527 | + type = StructAggregateEmbeddableIntegrationTests.Single[].class) |
| 528 | + }) |
| 529 | + public static class ItemWithArrayAndCollectionValues { |
| 530 | + public static final String MAPPING_FOR_ITEM = "ItemWithArrayAndCollectionValues"; |
| 531 | + |
492 | 532 | @Id |
493 | | - int id; |
| 533 | + public int id; |
494 | 534 |
|
495 | 535 | byte[] bytes; |
496 | 536 | char[] chars; |
@@ -519,7 +559,7 @@ static class ItemWithArrayAndCollectionValues { |
519 | 559 |
|
520 | 560 | ItemWithArrayAndCollectionValues() {} |
521 | 561 |
|
522 | | - ItemWithArrayAndCollectionValues( |
| 562 | + public ItemWithArrayAndCollectionValues( |
523 | 563 | int id, |
524 | 564 | byte[] bytes, |
525 | 565 | char[] chars, |
@@ -571,27 +611,73 @@ static class ItemWithArrayAndCollectionValues { |
571 | 611 | this.objectIdsCollection = objectIdsCollection; |
572 | 612 | this.structAggregateEmbeddablesCollection = structAggregateEmbeddablesCollection; |
573 | 613 | } |
| 614 | + |
| 615 | + public static Bson projectAll() { |
| 616 | + return project(include( |
| 617 | + ID_FIELD_NAME, |
| 618 | + "bytes", |
| 619 | + "chars", |
| 620 | + "ints", |
| 621 | + "longs", |
| 622 | + "doubles", |
| 623 | + "booleans", |
| 624 | + "boxedChars", |
| 625 | + "boxedInts", |
| 626 | + "boxedLongs", |
| 627 | + "boxedDoubles", |
| 628 | + "boxedBooleans", |
| 629 | + "strings", |
| 630 | + "bigDecimals", |
| 631 | + "objectIds", |
| 632 | + "structAggregateEmbeddables", |
| 633 | + "charsCollection", |
| 634 | + "intsCollection", |
| 635 | + "longsCollection", |
| 636 | + "doublesCollection", |
| 637 | + "booleansCollection", |
| 638 | + "stringsCollection", |
| 639 | + "bigDecimalsCollection", |
| 640 | + "objectIdsCollection", |
| 641 | + "structAggregateEmbeddablesCollection")); |
| 642 | + } |
574 | 643 | } |
575 | 644 |
|
576 | 645 | @Entity |
577 | 646 | @Table(name = COLLECTION_NAME) |
578 | | - static class ItemWithArrayAndCollectionValuesOfStructAggregateEmbeddablesHavingArraysAndCollections { |
| 647 | + @SqlResultSetMapping( |
| 648 | + name = |
| 649 | + ItemWithArrayAndCollectionValuesOfStructAggregateEmbeddablesHavingArraysAndCollections |
| 650 | + .MAPPING_FOR_ITEM, |
| 651 | + columns = { |
| 652 | + @ColumnResult(name = ID_FIELD_NAME), |
| 653 | + @ColumnResult(name = "structAggregateEmbeddables", type = ArraysAndCollections[].class), |
| 654 | + @ColumnResult(name = "structAggregateEmbeddablesCollection", type = ArraysAndCollections[].class) |
| 655 | + }) |
| 656 | + public static class ItemWithArrayAndCollectionValuesOfStructAggregateEmbeddablesHavingArraysAndCollections { |
| 657 | + public static final String MAPPING_FOR_ITEM = |
| 658 | + "ItemWithArrayAndCollectionValuesOfStructAggregateEmbeddablesHavingArraysAndCollections"; |
| 659 | + |
579 | 660 | @Id |
580 | | - int id; |
| 661 | + public int id; |
581 | 662 |
|
582 | 663 | ArraysAndCollections[] structAggregateEmbeddables; |
583 | 664 | Collection<ArraysAndCollections> structAggregateEmbeddablesCollection; |
584 | 665 |
|
585 | 666 | ItemWithArrayAndCollectionValuesOfStructAggregateEmbeddablesHavingArraysAndCollections() {} |
586 | 667 |
|
587 | | - ItemWithArrayAndCollectionValuesOfStructAggregateEmbeddablesHavingArraysAndCollections( |
| 668 | + public ItemWithArrayAndCollectionValuesOfStructAggregateEmbeddablesHavingArraysAndCollections( |
588 | 669 | int id, |
589 | 670 | ArraysAndCollections[] structAggregateEmbeddables, |
590 | 671 | Collection<ArraysAndCollections> structAggregateEmbeddablesCollection) { |
591 | 672 | this.id = id; |
592 | 673 | this.structAggregateEmbeddables = structAggregateEmbeddables; |
593 | 674 | this.structAggregateEmbeddablesCollection = structAggregateEmbeddablesCollection; |
594 | 675 | } |
| 676 | + |
| 677 | + public static Bson projectAll() { |
| 678 | + return project( |
| 679 | + include(ID_FIELD_NAME, "structAggregateEmbeddables", "structAggregateEmbeddablesCollection")); |
| 680 | + } |
595 | 681 | } |
596 | 682 |
|
597 | 683 | @Nested |
|
0 commit comments