-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-14981 Support null precedence with Envers Query API
- Loading branch information
Showing
8 changed files
with
163 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
hibernate-envers/src/main/java/org/hibernate/envers/query/order/NullPrecedence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.envers.query.order; | ||
|
||
/** | ||
* Defines the possible null handling modes. | ||
* | ||
* @author Chris Cranford | ||
*/ | ||
public enum NullPrecedence { | ||
/** | ||
* Null values will be rendered before non-null values. | ||
*/ | ||
FIRST, | ||
|
||
/** | ||
* Null values will be rendered after non-null values. | ||
*/ | ||
LAST | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...ers/src/test/java/org/hibernate/orm/test/envers/integration/query/NullPrecedenceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.orm.test.envers.integration.query; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.hibernate.envers.query.AuditEntity; | ||
import org.hibernate.envers.query.order.NullPrecedence; | ||
import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase; | ||
import org.hibernate.orm.test.envers.Priority; | ||
import org.hibernate.orm.test.envers.entities.StrIntTestEntity; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import org.hibernate.testing.TestForIssue; | ||
import org.hibernate.testing.transaction.TransactionUtil; | ||
|
||
/** | ||
* Tests for the {@link NullPrecedence} query option on order-bys. | ||
* | ||
* @author Chris Cranford | ||
*/ | ||
@TestForIssue( jiraKey = "HHH-14981" ) | ||
public class NullPrecedenceTest extends BaseEnversJPAFunctionalTestCase { | ||
|
||
Integer id1; | ||
Integer id2; | ||
|
||
@Override | ||
protected Class<?>[] getAnnotatedClasses() { | ||
return new Class<?>[] { StrIntTestEntity.class }; | ||
} | ||
|
||
@Test | ||
@Priority(10) | ||
public void initData() { | ||
// Revision 1 | ||
id1 = TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> { | ||
StrIntTestEntity entity1 = new StrIntTestEntity( null, 1 ); | ||
entityManager.persist( entity1 ); | ||
return entity1.getId(); | ||
} ); | ||
// Revision 2 | ||
id2 = TransactionUtil.doInJPA( this::entityManagerFactory, entityManager -> { | ||
StrIntTestEntity entity2 = new StrIntTestEntity( "two", 2 ); | ||
entityManager.persist( entity2 ); | ||
return entity2.getId(); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void testNullPrecedenceFirst() { | ||
List results = getAuditReader().createQuery().forRevisionsOfEntity( StrIntTestEntity.class, true, false ) | ||
.addProjection( AuditEntity.property( "number" ) ) | ||
.addOrder( AuditEntity.property( "str1" ).asc().nulls( NullPrecedence.FIRST ) ) | ||
.getResultList(); | ||
List<Integer> expected = new ArrayList<>(); | ||
expected.addAll( Arrays.asList( 1, 2 ) ); | ||
Assert.assertEquals( expected, results ); | ||
} | ||
|
||
@Test | ||
public void testNullPrecedenceLast() { | ||
List results = getAuditReader().createQuery().forRevisionsOfEntity( StrIntTestEntity.class, true, false ) | ||
.addProjection( AuditEntity.property( "number" ) ) | ||
.addOrder( AuditEntity.property( "str1" ).asc().nulls( NullPrecedence.LAST ) ) | ||
.getResultList(); | ||
List<Integer> expected = new ArrayList<>(); | ||
expected.addAll( Arrays.asList( 2, 1 ) ); | ||
Assert.assertEquals( expected, results ); | ||
} | ||
} |