Skip to content

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,29 @@

package com.mongodb.hibernate;

import static com.mongodb.client.model.Aggregates.project;
import static com.mongodb.client.model.Projections.include;
import static com.mongodb.hibernate.BasicCrudIntegrationTests.Item.CONSTRUCTOR_MAPPING_FOR_ITEM;
import static com.mongodb.hibernate.BasicCrudIntegrationTests.Item.MAPPING_FOR_ITEM;
import static com.mongodb.hibernate.MongoTestAssertions.assertEq;
import static com.mongodb.hibernate.internal.MongoConstants.ID_FIELD_NAME;
import static org.assertj.core.api.Assertions.assertThat;

import com.mongodb.client.MongoCollection;
import com.mongodb.hibernate.embeddable.EmbeddableIntegrationTests;
import com.mongodb.hibernate.embeddable.StructAggregateEmbeddableIntegrationTests;
import com.mongodb.hibernate.junit.InjectMongoCollection;
import com.mongodb.hibernate.junit.MongoExtension;
import jakarta.persistence.ColumnResult;
import jakarta.persistence.ConstructorResult;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.SqlResultSetMapping;
import jakarta.persistence.Table;
import java.math.BigDecimal;
import java.time.Instant;
import org.bson.BsonDocument;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
Expand All @@ -44,10 +55,9 @@
BasicCrudIntegrationTests.ItemDynamicallyUpdated.class,
})
@ExtendWith(MongoExtension.class)
class BasicCrudIntegrationTests implements SessionFactoryScopeAware {
private static final String COLLECTION_NAME = "items";
public class BasicCrudIntegrationTests implements SessionFactoryScopeAware {

@InjectMongoCollection(COLLECTION_NAME)
@InjectMongoCollection(Item.COLLECTION_NAME)
private static MongoCollection<BsonDocument> mongoCollection;

private SessionFactoryScope sessionFactoryScope;
Expand Down Expand Up @@ -374,30 +384,83 @@ private static void assertCollectionContainsExactly(String documentAsJsonObject)
assertThat(mongoCollection.find()).containsExactly(BsonDocument.parse(documentAsJsonObject));
}

/**
* This class should have persistent attributes of all the <a
* href="https://docs.jboss.org/hibernate/orm/6.6/userguide/html_single/Hibernate_User_Guide.html#basic">basic
* types</a> we support. When adding more persistent attributes to this class, we should do similar changes to
* {@link EmbeddableIntegrationTests.Plural}/{@link StructAggregateEmbeddableIntegrationTests.Plural},
* {@link EmbeddableIntegrationTests.ArraysAndCollections}/{@link StructAggregateEmbeddableIntegrationTests.ArraysAndCollections},
* {@link ArrayAndCollectionIntegrationTests.ItemWithArrayAndCollectionValues}.
*/
@Entity
@Table(name = COLLECTION_NAME)
static class Item {
@Id
int id;
@Table(name = Item.COLLECTION_NAME)
@SqlResultSetMapping(
name = CONSTRUCTOR_MAPPING_FOR_ITEM,
classes =
@ConstructorResult(
targetClass = Item.class,
columns = {
@ColumnResult(name = ID_FIELD_NAME, type = int.class),
@ColumnResult(name = "primitiveChar", type = char.class),
@ColumnResult(name = "primitiveInt", type = int.class),
@ColumnResult(name = "primitiveLong", type = long.class),
@ColumnResult(name = "primitiveDouble", type = double.class),
@ColumnResult(name = "primitiveBoolean", type = boolean.class),
@ColumnResult(name = "boxedChar", type = Character.class),
@ColumnResult(name = "boxedInt", type = Integer.class),
@ColumnResult(name = "boxedLong", type = Long.class),
@ColumnResult(name = "boxedDouble", type = Double.class),
@ColumnResult(name = "boxedBoolean", type = Boolean.class),
@ColumnResult(name = "string", type = String.class),
@ColumnResult(name = "bigDecimal", type = BigDecimal.class),
@ColumnResult(name = "objectId", type = ObjectId.class),
@ColumnResult(name = "instant", type = Instant.class),
}))
@SqlResultSetMapping(
name = MAPPING_FOR_ITEM,
columns = {
@ColumnResult(name = ID_FIELD_NAME),
@ColumnResult(name = "primitiveChar", type = char.class),
@ColumnResult(name = "primitiveInt"),
@ColumnResult(name = "primitiveLong"),
@ColumnResult(name = "primitiveDouble"),
@ColumnResult(name = "primitiveBoolean"),
@ColumnResult(name = "boxedChar", type = Character.class),
@ColumnResult(name = "boxedInt"),
@ColumnResult(name = "boxedLong"),
@ColumnResult(name = "boxedDouble"),
@ColumnResult(name = "boxedBoolean"),
@ColumnResult(name = "string"),
@ColumnResult(name = "bigDecimal"),
@ColumnResult(name = "objectId"),
@ColumnResult(name = "instant")
})
public static class Item {
public static final String COLLECTION_NAME = "items";
public static final String CONSTRUCTOR_MAPPING_FOR_ITEM = "ConstructorItem";
public static final String MAPPING_FOR_ITEM = "Item";

char primitiveChar;
int primitiveInt;
long primitiveLong;
double primitiveDouble;
boolean primitiveBoolean;
Character boxedChar;
Integer boxedInt;
Long boxedLong;
Double boxedDouble;
Boolean boxedBoolean;
String string;
BigDecimal bigDecimal;
ObjectId objectId;
Instant instant;
@Id
public int id;

public char primitiveChar;
public int primitiveInt;
public long primitiveLong;
public double primitiveDouble;
public boolean primitiveBoolean;
public Character boxedChar;
public Integer boxedInt;
public Long boxedLong;
public Double boxedDouble;
public Boolean boxedBoolean;
public String string;
public BigDecimal bigDecimal;
public ObjectId objectId;
public Instant instant;

Item() {}

Item(
public Item(
int id,
char primitiveChar,
int primitiveInt,
Expand Down Expand Up @@ -429,10 +492,29 @@ static class Item {
this.objectId = objectId;
this.instant = instant;
}

public static Bson projectAll() {
return project(include(
ID_FIELD_NAME,
"primitiveChar",
"primitiveInt",
"primitiveLong",
"primitiveDouble",
"primitiveBoolean",
"boxedChar",
"boxedInt",
"boxedLong",
"boxedDouble",
"boxedBoolean",
"string",
"bigDecimal",
"objectId",
"instant"));
}
}

@Entity
@Table(name = COLLECTION_NAME)
@Table(name = Item.COLLECTION_NAME)
static class ItemDynamicallyUpdated {
@Id
int id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.mongodb.hibernate.embeddable;

import static com.mongodb.hibernate.BasicCrudIntegrationTests.Item.COLLECTION_NAME;
import static com.mongodb.hibernate.MongoTestAssertions.assertEq;
import static com.mongodb.hibernate.MongoTestAssertions.assertUsingRecursiveComparison;
import static java.util.Arrays.asList;
Expand All @@ -25,6 +26,7 @@

import com.mongodb.client.MongoCollection;
import com.mongodb.hibernate.ArrayAndCollectionIntegrationTests;
import com.mongodb.hibernate.BasicCrudIntegrationTests;
import com.mongodb.hibernate.internal.FeatureNotSupportedException;
import com.mongodb.hibernate.junit.InjectMongoCollection;
import com.mongodb.hibernate.junit.MongoExtension;
Expand Down Expand Up @@ -64,8 +66,6 @@
})
@ExtendWith(MongoExtension.class)
public class EmbeddableIntegrationTests implements SessionFactoryScopeAware {
private static final String COLLECTION_NAME = "items";

@InjectMongoCollection(COLLECTION_NAME)
private static MongoCollection<BsonDocument> mongoCollection;

Expand Down Expand Up @@ -432,8 +432,32 @@ void testFlattenedValueHavingEmptyArraysAndCollections() {
@Test
public void testFlattenedValueHavingNullArraysAndCollections() {
var emptyEmbeddable = new ArraysAndCollections(
null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null, null);
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
(List<Character>) null,
null,
null,
null,
null,
null,
null,
null,
null,
null);
var item = new ItemWithFlattenedValueHavingArraysAndCollections(1, emptyEmbeddable);
sessionFactoryScope.inTransaction(session -> session.persist(item));
assertCollectionContainsExactly(
Expand Down Expand Up @@ -601,8 +625,9 @@ ItemWithFlattenedValues getParent() {
}
}

/** @see BasicCrudIntegrationTests.Item */
@Embeddable
record Plural(
public record Plural(
char primitiveChar,
int primitiveInt,
long primitiveLong,
Expand Down Expand Up @@ -634,8 +659,9 @@ static class ItemWithFlattenedValueHavingArraysAndCollections {
}
}

/** @see BasicCrudIntegrationTests.Item */
@Embeddable
static class ArraysAndCollections {
public static class ArraysAndCollections {
byte[] bytes;
char[] chars;
int[] ints;
Expand Down Expand Up @@ -665,7 +691,7 @@ static class ArraysAndCollections {

ArraysAndCollections() {}

ArraysAndCollections(
public ArraysAndCollections(
byte[] bytes,
char[] chars,
int[] ints,
Expand Down Expand Up @@ -719,6 +745,62 @@ static class ArraysAndCollections {
this.instantsCollection = instantsCollection;
this.structAggregateEmbeddablesCollection = structAggregateEmbeddablesCollection;
}

ArraysAndCollections(
byte[] bytes,
char[] chars,
int[] ints,
long[] longs,
double[] doubles,
boolean[] booleans,
Character[] boxedChars,
Integer[] boxedInts,
Long[] boxedLongs,
Double[] boxedDoubles,
Boolean[] boxedBooleans,
String[] strings,
BigDecimal[] bigDecimals,
ObjectId[] objectIds,
Instant[] instants,
StructAggregateEmbeddableIntegrationTests.Single[] structAggregateEmbeddables,
Character[] charsCollection,
Integer[] intsCollection,
Long[] longsCollection,
Double[] doublesCollection,
Boolean[] booleansCollection,
String[] stringsCollection,
BigDecimal[] bigDecimalsCollection,
ObjectId[] objectIdsCollection,
Instant[] instantsCollection,
StructAggregateEmbeddableIntegrationTests.Single[] structAggregateEmbeddablesCollection) {
this(
bytes,
chars,
ints,
longs,
doubles,
booleans,
boxedChars,
boxedInts,
boxedLongs,
boxedDoubles,
boxedBooleans,
strings,
bigDecimals,
objectIds,
instants,
structAggregateEmbeddables,
charsCollection == null ? null : asList(charsCollection),
intsCollection == null ? null : new HashSet<>(asList(intsCollection)),
longsCollection == null ? null : asList(longsCollection),
doublesCollection == null ? null : asList(doublesCollection),
booleansCollection == null ? null : asList(booleansCollection),
stringsCollection == null ? null : asList(stringsCollection),
bigDecimalsCollection == null ? null : asList(bigDecimalsCollection),
objectIdsCollection == null ? null : asList(objectIdsCollection),
instantsCollection == null ? null : asList(instantsCollection),
structAggregateEmbeddablesCollection == null ? null : asList(structAggregateEmbeddablesCollection));
}
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package com.mongodb.hibernate.embeddable;

import static com.mongodb.hibernate.BasicCrudIntegrationTests.Item.COLLECTION_NAME;
import static com.mongodb.hibernate.MongoTestAssertions.assertEq;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.mongodb.client.MongoCollection;
import com.mongodb.hibernate.ArrayAndCollectionIntegrationTests;
import com.mongodb.hibernate.BasicCrudIntegrationTests;
import com.mongodb.hibernate.internal.FeatureNotSupportedException;
import com.mongodb.hibernate.junit.InjectMongoCollection;
import com.mongodb.hibernate.junit.MongoExtension;
Expand Down Expand Up @@ -63,8 +65,6 @@
})
@ExtendWith(MongoExtension.class)
public class StructAggregateEmbeddableIntegrationTests implements SessionFactoryScopeAware {
private static final String COLLECTION_NAME = "items";

@InjectMongoCollection(COLLECTION_NAME)
private static MongoCollection<BsonDocument> mongoCollection;

Expand Down Expand Up @@ -597,9 +597,10 @@ ItemWithNestedValues getParent() {
}
}

/** @see BasicCrudIntegrationTests.Item */
@Embeddable
@Struct(name = "Plural")
record Plural(
public record Plural(
char primitiveChar,
int primitiveInt,
long primitiveLong,
Expand Down Expand Up @@ -631,6 +632,7 @@ static class ItemWithNestedValueHavingArraysAndCollections {
}
}

/** @see BasicCrudIntegrationTests.Item */
@Embeddable
@Struct(name = "ArraysAndCollections")
public static class ArraysAndCollections {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static com.mongodb.hibernate.MongoTestAssertions.assertIterableEq;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.hibernate.cfg.JdbcSettings.DIALECT;
import static org.hibernate.cfg.AvailableSettings.DIALECT;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.spy;
Expand Down
Loading