Fix SqmMemberOfPredicate negation determination

This commit is contained in:
Andrea Boriero 2022-02-04 13:56:07 +01:00 committed by Andrea Boriero
parent 9a2c87808b
commit f7d5bc857b
2 changed files with 85 additions and 18 deletions

View File

@ -1965,7 +1965,7 @@ public class SemanticQueryBuilder<R> extends HqlParserBaseVisitor<Object> implem
@Override @Override
public SqmPredicate visitMemberOfPredicate(HqlParser.MemberOfPredicateContext ctx) { public SqmPredicate visitMemberOfPredicate(HqlParser.MemberOfPredicateContext ctx) {
final boolean negated = ctx.getChildCount() == 5; final boolean negated = ctx.NOT() != null;
final SqmPath<?> sqmPluralPath = consumeDomainPath( final SqmPath<?> sqmPluralPath = consumeDomainPath(
(HqlParser.PathContext) ctx.getChild( ctx.getChildCount() - 1 ) (HqlParser.PathContext) ctx.getChild( ctx.getChildCount() - 1 )
); );

View File

@ -14,6 +14,14 @@ import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.CascadeType; import jakarta.persistence.CascadeType;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection; import jakarta.persistence.ElementCollection;
@ -30,12 +38,6 @@ import jakarta.persistence.Table;
import jakarta.persistence.Temporal; import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType; import jakarta.persistence.TemporalType;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -107,6 +109,17 @@ public class OneToManyHqlMemberOfQueryTest {
); );
} }
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from Call" ).executeUpdate();
session.createQuery( "delete from Phone" ).executeUpdate();
session.createQuery( "delete from Person" ).executeUpdate();
}
);
}
@Test @Test
public void testMemberOf(SessionFactoryScope scope) { public void testMemberOf(SessionFactoryScope scope) {
scope.inTransaction( scope.inTransaction(
@ -114,13 +127,67 @@ public class OneToManyHqlMemberOfQueryTest {
Call call = session.createQuery( "select c from Call c", Call.class ).getResultList().get( 0 ); Call call = session.createQuery( "select c from Call c", Call.class ).getResultList().get( 0 );
Phone phone = call.getPhone(); Phone phone = call.getPhone();
List<Person> persons = session.createQuery( List<Person> people = session.createQuery(
"select p " + "select p " +
"from Person p " + "from Person p " +
"where :phone member of p.phones", Person.class ) "where :phone member of p.phones", Person.class )
.setParameter( "phone", phone ) .setParameter( "phone", phone )
.getResultList(); .getResultList();
assertEquals( 1, persons.size() ); assertEquals( 1, people.size() );
}
);
}
@Test
public void testNegatedMemberOf(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Call call = session.createQuery( "select c from Call c", Call.class ).getResultList().get( 0 );
Phone phone = call.getPhone();
List<Person> people = session.createQuery(
"select p " +
"from Person p " +
"where :phone not member of p.phones", Person.class )
.setParameter( "phone", phone )
.getResultList();
assertEquals( 2, people.size() );
}
);
}
@Test
public void testMember(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Call call = session.createQuery( "select c from Call c", Call.class ).getResultList().get( 0 );
Phone phone = call.getPhone();
List<Person> people = session.createQuery(
"select p " +
"from Person p " +
"where :phone member p.phones", Person.class )
.setParameter( "phone", phone )
.getResultList();
assertEquals( 1, people.size() );
}
);
}
@Test
public void testNegatedMember(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Call call = session.createQuery( "select c from Call c", Call.class ).getResultList().get( 0 );
Phone phone = call.getPhone();
List<Person> people = session.createQuery(
"select p " +
"from Person p " +
"where :phone not member p.phones", Person.class )
.setParameter( "phone", phone )
.getResultList();
assertEquals( 2, people.size() );
} }
); );
} }
@ -131,14 +198,14 @@ public class OneToManyHqlMemberOfQueryTest {
session -> { session -> {
Call call = session.createQuery( "select c from Call c", Call.class ).getResultList().get( 0 ); Call call = session.createQuery( "select c from Call c", Call.class ).getResultList().get( 0 );
List<Person> persons = session.createQuery( List<Person> people = session.createQuery(
"select p " + "select p " +
"from Person p " + "from Person p " +
"join p.phones phone " + "join p.phones phone " +
"where :call member of phone.calls", Person.class ) "where :call member of phone.calls", Person.class )
.setParameter( "call", call ) .setParameter( "call", call )
.getResultList(); .getResultList();
assertEquals( 1, persons.size() ); assertEquals( 1, people.size() );
} }
); );
} }