Skip to content

Commit 6fb9e8c

Browse files
committed
1 parent 1396799 commit 6fb9e8c

18 files changed

+1148
-80
lines changed

src/integrationTest/java/com/mongodb/hibernate/ArrayAndCollectionIntegrationTests.java

Lines changed: 98 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
package com.mongodb.hibernate;
1818

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;
1922
import static com.mongodb.hibernate.MongoTestAssertions.assertEq;
23+
import static com.mongodb.hibernate.internal.MongoConstants.ID_FIELD_NAME;
2024
import static java.util.Arrays.asList;
2125
import static org.assertj.core.api.Assertions.assertThat;
2226
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -29,16 +33,19 @@
2933
import com.mongodb.hibernate.internal.FeatureNotSupportedException;
3034
import com.mongodb.hibernate.junit.InjectMongoCollection;
3135
import com.mongodb.hibernate.junit.MongoExtension;
36+
import jakarta.persistence.ColumnResult;
3237
import jakarta.persistence.ElementCollection;
3338
import jakarta.persistence.Entity;
3439
import jakarta.persistence.Id;
40+
import jakarta.persistence.SqlResultSetMapping;
3541
import jakarta.persistence.Table;
3642
import java.math.BigDecimal;
3743
import java.sql.SQLFeatureNotSupportedException;
3844
import java.util.Collection;
39-
import java.util.LinkedHashSet;
45+
import java.util.HashSet;
4046
import java.util.List;
4147
import org.bson.BsonDocument;
48+
import org.bson.conversions.Bson;
4249
import org.bson.types.ObjectId;
4350
import org.hibernate.MappingException;
4451
import org.hibernate.boot.MetadataSources;
@@ -64,8 +71,6 @@
6471
@ServiceRegistry(settings = {@Setting(name = WRAPPER_ARRAY_HANDLING, value = "allow")})
6572
@ExtendWith(MongoExtension.class)
6673
public class ArrayAndCollectionIntegrationTests implements SessionFactoryScopeAware {
67-
private static final String COLLECTION_NAME = "items";
68-
6974
@InjectMongoCollection(COLLECTION_NAME)
7075
private static MongoCollection<BsonDocument> mongoCollection;
7176

@@ -98,7 +103,7 @@ void testArrayAndCollectionValues() {
98103
new StructAggregateEmbeddableIntegrationTests.Single(1), null
99104
},
100105
asList('s', 't', null, 'r'),
101-
asList(null, 5),
106+
new HashSet<>(asList(null, 5)),
102107
asList(Long.MAX_VALUE, null, 6L),
103108
asList(null, Double.MAX_VALUE),
104109
asList(null, true),
@@ -310,8 +315,7 @@ void testArrayAndCollectionValuesOfStructAggregateEmbeddablesHavingArraysAndColl
310315
new StructAggregateEmbeddableIntegrationTests.Single(1)
311316
},
312317
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)),
315319
List.of(Long.MAX_VALUE, 6L),
316320
List.of(Double.MAX_VALUE),
317321
List.of(true),
@@ -486,11 +490,47 @@ private static void assertCollectionContainsExactly(String documentAsJsonObject)
486490
assertThat(mongoCollection.find()).containsExactly(BsonDocument.parse(documentAsJsonObject));
487491
}
488492

493+
/** @see BasicCrudIntegrationTests.Item */
489494
@Entity
490495
@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+
492532
@Id
493-
int id;
533+
public int id;
494534

495535
byte[] bytes;
496536
char[] chars;
@@ -519,7 +559,7 @@ static class ItemWithArrayAndCollectionValues {
519559

520560
ItemWithArrayAndCollectionValues() {}
521561

522-
ItemWithArrayAndCollectionValues(
562+
public ItemWithArrayAndCollectionValues(
523563
int id,
524564
byte[] bytes,
525565
char[] chars,
@@ -571,27 +611,73 @@ static class ItemWithArrayAndCollectionValues {
571611
this.objectIdsCollection = objectIdsCollection;
572612
this.structAggregateEmbeddablesCollection = structAggregateEmbeddablesCollection;
573613
}
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+
}
574643
}
575644

576645
@Entity
577646
@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+
579660
@Id
580-
int id;
661+
public int id;
581662

582663
ArraysAndCollections[] structAggregateEmbeddables;
583664
Collection<ArraysAndCollections> structAggregateEmbeddablesCollection;
584665

585666
ItemWithArrayAndCollectionValuesOfStructAggregateEmbeddablesHavingArraysAndCollections() {}
586667

587-
ItemWithArrayAndCollectionValuesOfStructAggregateEmbeddablesHavingArraysAndCollections(
668+
public ItemWithArrayAndCollectionValuesOfStructAggregateEmbeddablesHavingArraysAndCollections(
588669
int id,
589670
ArraysAndCollections[] structAggregateEmbeddables,
590671
Collection<ArraysAndCollections> structAggregateEmbeddablesCollection) {
591672
this.id = id;
592673
this.structAggregateEmbeddables = structAggregateEmbeddables;
593674
this.structAggregateEmbeddablesCollection = structAggregateEmbeddablesCollection;
594675
}
676+
677+
public static Bson projectAll() {
678+
return project(
679+
include(ID_FIELD_NAME, "structAggregateEmbeddables", "structAggregateEmbeddablesCollection"));
680+
}
595681
}
596682

597683
@Nested

src/integrationTest/java/com/mongodb/hibernate/BasicCrudIntegrationTests.java

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,26 @@
1616

1717
package com.mongodb.hibernate;
1818

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.MAPPING_FOR_ITEM;
1922
import static com.mongodb.hibernate.MongoTestAssertions.assertEq;
23+
import static com.mongodb.hibernate.internal.MongoConstants.ID_FIELD_NAME;
2024
import static org.assertj.core.api.Assertions.assertThat;
2125

2226
import com.mongodb.client.MongoCollection;
27+
import com.mongodb.hibernate.embeddable.EmbeddableIntegrationTests;
28+
import com.mongodb.hibernate.embeddable.StructAggregateEmbeddableIntegrationTests;
2329
import com.mongodb.hibernate.junit.InjectMongoCollection;
2430
import com.mongodb.hibernate.junit.MongoExtension;
31+
import jakarta.persistence.ColumnResult;
2532
import jakarta.persistence.Entity;
2633
import jakarta.persistence.Id;
34+
import jakarta.persistence.SqlResultSetMapping;
2735
import jakarta.persistence.Table;
2836
import java.math.BigDecimal;
2937
import org.bson.BsonDocument;
38+
import org.bson.conversions.Bson;
3039
import org.bson.types.ObjectId;
3140
import org.hibernate.testing.orm.junit.DomainModel;
3241
import org.hibernate.testing.orm.junit.SessionFactory;
@@ -43,10 +52,9 @@
4352
BasicCrudIntegrationTests.ItemDynamicallyUpdated.class,
4453
})
4554
@ExtendWith(MongoExtension.class)
46-
class BasicCrudIntegrationTests implements SessionFactoryScopeAware {
47-
private static final String COLLECTION_NAME = "items";
55+
public class BasicCrudIntegrationTests implements SessionFactoryScopeAware {
4856

49-
@InjectMongoCollection(COLLECTION_NAME)
57+
@InjectMongoCollection(Item.COLLECTION_NAME)
5058
private static MongoCollection<BsonDocument> mongoCollection;
5159

5260
private SessionFactoryScope sessionFactoryScope;
@@ -348,29 +356,58 @@ private static void assertCollectionContainsExactly(String documentAsJsonObject)
348356
assertThat(mongoCollection.find()).containsExactly(BsonDocument.parse(documentAsJsonObject));
349357
}
350358

359+
/**
360+
* This class should have persistent attributes of all the <a
361+
* href="https://docs.jboss.org/hibernate/orm/6.6/userguide/html_single/Hibernate_User_Guide.html#basic">basic
362+
* types</a> we support. When adding more persistent attributes to this class, we should do similar changes to
363+
* {@link EmbeddableIntegrationTests.Plural}/{@link StructAggregateEmbeddableIntegrationTests.Plural},
364+
* {@link EmbeddableIntegrationTests.ArraysAndCollections}/{@link StructAggregateEmbeddableIntegrationTests.ArraysAndCollections},
365+
* {@link ArrayAndCollectionIntegrationTests.ItemWithArrayAndCollectionValues}.
366+
*/
351367
@Entity
352-
@Table(name = COLLECTION_NAME)
353-
static class Item {
354-
@Id
355-
int id;
368+
@Table(name = Item.COLLECTION_NAME)
369+
@SqlResultSetMapping(
370+
name = MAPPING_FOR_ITEM,
371+
columns = {
372+
@ColumnResult(name = ID_FIELD_NAME),
373+
@ColumnResult(name = "primitiveChar", type = char.class),
374+
@ColumnResult(name = "primitiveInt"),
375+
@ColumnResult(name = "primitiveLong"),
376+
@ColumnResult(name = "primitiveDouble"),
377+
@ColumnResult(name = "primitiveBoolean"),
378+
@ColumnResult(name = "boxedChar", type = Character.class),
379+
@ColumnResult(name = "boxedInt"),
380+
@ColumnResult(name = "boxedLong"),
381+
@ColumnResult(name = "boxedDouble"),
382+
@ColumnResult(name = "boxedBoolean"),
383+
@ColumnResult(name = "string"),
384+
@ColumnResult(name = "bigDecimal"),
385+
@ColumnResult(name = "objectId")
386+
})
387+
public static class Item {
388+
public static final String COLLECTION_NAME = "items";
389+
public static final String MAPPING_FOR_ITEM = "Item";
356390

357-
char primitiveChar;
358-
int primitiveInt;
359-
long primitiveLong;
360-
double primitiveDouble;
361-
boolean primitiveBoolean;
362-
Character boxedChar;
363-
Integer boxedInt;
364-
Long boxedLong;
365-
Double boxedDouble;
366-
Boolean boxedBoolean;
367-
String string;
368-
BigDecimal bigDecimal;
369-
ObjectId objectId;
391+
@Id
392+
public int id;
393+
394+
public char primitiveChar;
395+
public int primitiveInt;
396+
public long primitiveLong;
397+
public double primitiveDouble;
398+
public boolean primitiveBoolean;
399+
public Character boxedChar;
400+
public Integer boxedInt;
401+
public Long boxedLong;
402+
public Double boxedDouble;
403+
public Boolean boxedBoolean;
404+
public String string;
405+
public BigDecimal bigDecimal;
406+
public ObjectId objectId;
370407

371408
Item() {}
372409

373-
Item(
410+
public Item(
374411
int id,
375412
char primitiveChar,
376413
int primitiveInt,
@@ -400,10 +437,28 @@ static class Item {
400437
this.bigDecimal = bigDecimal;
401438
this.objectId = objectId;
402439
}
440+
441+
public static Bson projectAll() {
442+
return project(include(
443+
ID_FIELD_NAME,
444+
"primitiveChar",
445+
"primitiveInt",
446+
"primitiveLong",
447+
"primitiveDouble",
448+
"primitiveBoolean",
449+
"boxedChar",
450+
"boxedInt",
451+
"boxedLong",
452+
"boxedDouble",
453+
"boxedBoolean",
454+
"string",
455+
"bigDecimal",
456+
"objectId"));
457+
}
403458
}
404459

405460
@Entity
406-
@Table(name = COLLECTION_NAME)
461+
@Table(name = Item.COLLECTION_NAME)
407462
static class ItemDynamicallyUpdated {
408463
@Id
409464
int id;

src/integrationTest/java/com/mongodb/hibernate/embeddable/EmbeddableIntegrationTests.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.mongodb.hibernate.embeddable;
1818

19+
import static com.mongodb.hibernate.BasicCrudIntegrationTests.Item.COLLECTION_NAME;
1920
import static com.mongodb.hibernate.MongoTestAssertions.assertEq;
2021
import static com.mongodb.hibernate.MongoTestAssertions.assertUsingRecursiveComparison;
2122
import static java.util.Arrays.asList;
@@ -25,6 +26,7 @@
2526

2627
import com.mongodb.client.MongoCollection;
2728
import com.mongodb.hibernate.ArrayAndCollectionIntegrationTests;
29+
import com.mongodb.hibernate.BasicCrudIntegrationTests;
2830
import com.mongodb.hibernate.internal.FeatureNotSupportedException;
2931
import com.mongodb.hibernate.junit.InjectMongoCollection;
3032
import com.mongodb.hibernate.junit.MongoExtension;
@@ -63,8 +65,6 @@
6365
})
6466
@ExtendWith(MongoExtension.class)
6567
public class EmbeddableIntegrationTests implements SessionFactoryScopeAware {
66-
private static final String COLLECTION_NAME = "items";
67-
6868
@InjectMongoCollection(COLLECTION_NAME)
6969
private static MongoCollection<BsonDocument> mongoCollection;
7070

@@ -581,8 +581,9 @@ ItemWithFlattenedValues getParent() {
581581
}
582582
}
583583

584+
/** @see BasicCrudIntegrationTests.Item */
584585
@Embeddable
585-
record Plural(
586+
public record Plural(
586587
char primitiveChar,
587588
int primitiveInt,
588589
long primitiveLong,
@@ -613,8 +614,9 @@ static class ItemWithFlattenedValueHavingArraysAndCollections {
613614
}
614615
}
615616

617+
/** @see BasicCrudIntegrationTests.Item */
616618
@Embeddable
617-
static class ArraysAndCollections {
619+
public static class ArraysAndCollections {
618620
byte[] bytes;
619621
char[] chars;
620622
int[] ints;
@@ -642,7 +644,7 @@ static class ArraysAndCollections {
642644

643645
ArraysAndCollections() {}
644646

645-
ArraysAndCollections(
647+
public ArraysAndCollections(
646648
byte[] bytes,
647649
char[] chars,
648650
int[] ints,

0 commit comments

Comments
 (0)