Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/integrationTest/java/com/mongodb/hibernate/query/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ public Book(int id, String title, Integer publishYear, Boolean outOfStock) {
this.outOfStock = outOfStock;
}

public Book(
int id,
String title,
Integer publishYear,
Boolean outOfStock,
Long isbn13,
Double discount,
BigDecimal price) {
this(id, title, publishYear, outOfStock);
this.isbn13 = isbn13;
this.discount = discount;
this.price = price;
}

@Override
public String toString() {
return "Book{" + "id=" + id + '}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,18 @@ void testDeletionWithNonZeroMutationCount() {
{
"limit": 0,
"q": {
"title": {
"$eq": "War and Peace"
}
"$and": [
{
"title": {
"$eq": "War and Peace"
}
},
{
"title": {
"$ne": null
}
}
]
}
}
]
Expand Down Expand Up @@ -121,9 +130,18 @@ void testDeletionWithZeroMutationCount() {
{
"limit": 0,
"q": {
"publishYear": {
"$lt": 1850
}
"$and": [
{
"publishYear": {
"$lt": 1850
}
},
{
"publishYear": {
"$ne": null
}
}
]
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,18 @@ void testUpdateWithNonZeroMutationCount() {
{
"multi": true,
"q": {
"title": {
"$eq": "War & Peace"
}
"$and": [
{
"title": {
"$eq": "War & Peace"
}
},
{
"title": {
"$ne": null
}
}
]
},
"u": {
"$set": {
Expand Down Expand Up @@ -151,9 +160,18 @@ void testUpdateWithZeroMutationCount() {
{
"multi": true,
"q": {
"publishYear": {
"$lt": 1850
}
"$and": [
{
"publishYear": {
"$lt": 1850
}
},
{
"publishYear": {
"$ne": null
}
}
]
},
"u": {
"$set": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,18 @@ void testBooleanFieldPathExpression(boolean negated) {
"pipeline": [
{
"$match": {
"outOfStock": {
"$eq": %s
}
"$and": [
{
"outOfStock": {
"$eq": %s
}
},
{
"outOfStock": {
"$ne": null
}
}
]
}
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright 2025-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mongodb.hibernate.query.select;

import com.mongodb.hibernate.query.AbstractQueryIntegrationTests;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import org.hibernate.testing.orm.junit.DomainModel;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@DomainModel(annotatedClasses = {NullnessPredicateTranslationIntegrationTests.Book.class})
class NullnessPredicateTranslationIntegrationTests extends AbstractQueryIntegrationTests {

@BeforeEach
void beforeEach() {
getSessionFactoryScope().inTransaction(session -> testingBooks.forEach(session::persist));
getTestCommandListener().clear();
}

private static final List<Book> testingBooks = List.of(
new Book(1, "War and Peace", null),
new Book(2, null, null),
new Book(3, "The Brothers Karamazov", 1880),
new Book(4, "War and Peace", 1867),
new Book(5, null, null));

private static List<Book> getBooksByIds(int... ids) {
return Arrays.stream(ids)
.mapToObj(id -> testingBooks.stream()
.filter(c -> c.id == id)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("id does not exist: " + id)))
.toList();
}

@ParameterizedTest
@ValueSource(booleans = {true, false})
void testNonEmbeddable(boolean isNegated) {
assertSelectionQuery(
"from Book where title = 'War and Peace' and publishYear " + (isNegated ? "is not null" : "is null"),
Book.class,
"""
{
"aggregate": "books",
"pipeline": [
{
"$match": {
"$and": [
{
"$and": [
{
"title": {
"$eq": "War and Peace"
}
},
{
"title": {
"$ne": null
}
}
]
},
{
"publishYear": {
"%s": null
}
}
]
}
},
{
"$project": {
"_id": true,
"publishYear": true,
"title": true
}
}
]
}"""
.formatted(isNegated ? "$ne" : "$eq"),
isNegated ? getBooksByIds(4) : getBooksByIds(1),
Set.of("books"));
}

@Entity(name = "Book")
@Table(name = "books")
static class Book {
@Id
int id;

String title;

Integer publishYear;

public Book() {}

public Book(int id, String title, Integer publishYear) {
this.id = id;
this.title = title;
this.publishYear = publishYear;
}

@Override
public String toString() {
return "Book{" + "id=" + id + '}';
}
}
}
Loading