Java Code Examples for org.hibernate.NullPrecedence

The following examples show how to use org.hibernate.NullPrecedence. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: lams   Source File: MySQLDialect.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public String renderOrderByElement(String expression, String collation, String order, NullPrecedence nulls) {
	final StringBuilder orderByElement = new StringBuilder();
	if ( nulls != NullPrecedence.NONE ) {
		// Workaround for NULLS FIRST / LAST support.
		orderByElement.append( "case when " ).append( expression ).append( " is null then " );
		if ( nulls == NullPrecedence.FIRST ) {
			orderByElement.append( "0 else 1" );
		}
		else {
			orderByElement.append( "1 else 0" );
		}
		orderByElement.append( " end, " );
	}
	// Nulls precedence has already been handled so passing NONE value.
	orderByElement.append( super.renderOrderByElement( expression, collation, order, NullPrecedence.NONE ) );
	return orderByElement.toString();
}
 
Example 2
Source Project: lams   Source File: SQLServer2008Dialect.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public String renderOrderByElement(String expression, String collation, String order, NullPrecedence nulls) {
	final StringBuilder orderByElement = new StringBuilder();

	if ( nulls != null && !NullPrecedence.NONE.equals( nulls ) ) {
		// Workaround for NULLS FIRST / LAST support.
		orderByElement.append( "case when " ).append( expression ).append( " is null then " );
		if ( NullPrecedence.FIRST.equals( nulls ) ) {
			orderByElement.append( "0 else 1" );
		}
		else {
			orderByElement.append( "1 else 0" );
		}
		orderByElement.append( " end, " );
	}

	// Nulls precedence has already been handled so passing NONE value.
	orderByElement.append( super.renderOrderByElement( expression, collation, order, NullPrecedence.NONE ) );

	return orderByElement.toString();
}
 
Example 3
Source Project: lams   Source File: DB2Dialect.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Handle DB2 "support" for null precedence...
 *
 * @param expression The SQL order expression. In case of {@code @OrderBy} annotation user receives property placeholder
 * (e.g. attribute name enclosed in '{' and '}' signs).
 * @param collation Collation string in format {@code collate IDENTIFIER}, or {@code null}
 * if expression has not been explicitly specified.
 * @param order Order direction. Possible values: {@code asc}, {@code desc}, or {@code null}
 * if expression has not been explicitly specified.
 * @param nullPrecedence Nulls precedence. Default value: {@link NullPrecedence#NONE}.
 *
 * @return
 */
@Override
public String renderOrderByElement(String expression, String collation, String order, NullPrecedence nullPrecedence) {
	if ( nullPrecedence == null || nullPrecedence == NullPrecedence.NONE ) {
		return super.renderOrderByElement( expression, collation, order, NullPrecedence.NONE );
	}

	// DB2 FTW!  A null precedence was explicitly requested, but DB2 "support" for null precedence
	// is a joke.  Basically it supports combos that align with what it does anyway.  Here is the
	// support matrix:
	//		* ASC + NULLS FIRST -> case statement
	//		* ASC + NULLS LAST -> just drop the NULLS LAST from sql fragment
	//		* DESC + NULLS FIRST -> just drop the NULLS FIRST from sql fragment
	//		* DESC + NULLS LAST -> case statement

	if ( ( nullPrecedence == NullPrecedence.FIRST  && "desc".equalsIgnoreCase( order ) )
			|| ( nullPrecedence == NullPrecedence.LAST && "asc".equalsIgnoreCase( order ) ) ) {
		// we have one of:
		//		* ASC + NULLS LAST
		//		* DESC + NULLS FIRST
		// so just drop the null precedence.  *NOTE: we could pass along the null precedence here,
		// but only DB2 9.7 or greater understand it; dropping it is more portable across DB2 versions
		return super.renderOrderByElement( expression, collation, order, NullPrecedence.NONE );
	}

	return String.format(
			Locale.ENGLISH,
			"case when %s is null then %s else %s end, %s %s",
			expression,
			nullPrecedence == NullPrecedence.FIRST ? "0" : "1",
			nullPrecedence == NullPrecedence.FIRST ? "1" : "0",
			expression,
			order
	);
}
 
Example 4
Source Project: lams   Source File: Dialect.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Renders an ordering fragment
 *
 * @param expression The SQL order expression. In case of {@code @OrderBy} annotation user receives property placeholder
 * (e.g. attribute name enclosed in '{' and '}' signs).
 * @param collation Collation string in format {@code collate IDENTIFIER}, or {@code null}
 * if expression has not been explicitly specified.
 * @param order Order direction. Possible values: {@code asc}, {@code desc}, or {@code null}
 * if expression has not been explicitly specified.
 * @param nulls Nulls precedence. Default value: {@link NullPrecedence#NONE}.
 * @return Renders single element of {@code ORDER BY} clause.
 */
public String renderOrderByElement(String expression, String collation, String order, NullPrecedence nulls) {
	final StringBuilder orderByElement = new StringBuilder( expression );
	if ( collation != null ) {
		orderByElement.append( " " ).append( collation );
	}
	if ( order != null ) {
		orderByElement.append( " " ).append( order );
	}
	if ( nulls != NullPrecedence.NONE ) {
		orderByElement.append( " nulls " ).append( nulls.name().toLowerCase( Locale.ROOT ) );
	}
	return orderByElement.toString();
}
 
Example 5
Source Project: lams   Source File: OrderByFragmentRenderer.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
protected String renderOrderByElement(String expression, String collation, String order, String nulls) {
	final NullPrecedence nullPrecedence = NullPrecedence.parse(
			nulls,
			sessionFactory.getSessionFactoryOptions().getDefaultNullPrecedence()
	);
	return sessionFactory.getDialect().renderOrderByElement( expression, collation, order, nullPrecedence );
}
 
Example 6
Source Project: lams   Source File: SqlGenerator.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
protected String renderOrderByElement(String expression, String order, String nulls) {
	final NullPrecedence nullPrecedence = NullPrecedence.parse( nulls,
																sessionFactory.getSettings()
																		.getDefaultNullPrecedence()
	);
	return sessionFactory.getDialect().renderOrderByElement( expression, null, order, nullPrecedence );
}
 
Example 7
Source Project: tutorials   Source File: FooSortingPersistenceIntegrationTest.java    License: MIT License 5 votes vote down vote up
@Test
public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
    final Criteria criteria = session.createCriteria(Foo.class, "FOO");
    criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST));
    final List<Foo> fooList = criteria.list();
    assertNull(fooList.get(fooList.toArray().length - 1).getName());
    for (final Foo foo : fooList) {
        System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
    }
}
 
Example 8
Source Project: tutorials   Source File: FooSortingPersistenceIntegrationTest.java    License: MIT License 5 votes vote down vote up
@Test
public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() {
    final Criteria criteria = session.createCriteria(Foo.class, "FOO");
    criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST));
    final List<Foo> fooList = criteria.list();
    assertNull(fooList.get(0).getName());
    for (final Foo foo : fooList) {
        System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
    }
}
 
Example 9
Source Project: lams   Source File: Settings.java    License: GNU General Public License v2.0 4 votes vote down vote up
public NullPrecedence getDefaultNullPrecedence() {
	return sessionFactoryOptions.getDefaultNullPrecedence();
}
 
Example 10
@Override
public NullPrecedence getDefaultNullPrecedence() {
	return delegate.getDefaultNullPrecedence();
}
 
Example 11
@Override
public T applyDefaultNullPrecedence(NullPrecedence nullPrecedence) {
	delegate.applyDefaultNullPrecedence( nullPrecedence );
	return getThis();
}
 
Example 12
@Override
public NullPrecedence getDefaultNullPrecedence() {
	return defaultNullPrecedence;
}
 
Example 13
public void applyDefaultNullPrecedence(NullPrecedence nullPrecedence) {
	this.defaultNullPrecedence = nullPrecedence;
}
 
Example 14
@Override
public SessionFactoryBuilder applyDefaultNullPrecedence(NullPrecedence nullPrecedence) {
	this.optionsBuilder.applyDefaultNullPrecedence( nullPrecedence );
	return this;
}
 
Example 15
Source Project: keycloak   Source File: DelegatingDialect.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public String renderOrderByElement(String expression, String collation, String order, NullPrecedence nulls) {
    return getInstance().renderOrderByElement(expression, collation, order, nulls);
}
 
Example 16
Source Project: lams   Source File: Order.java    License: GNU General Public License v2.0 2 votes vote down vote up
/**
 * Defines precedence for nulls.
 *
 * @param nullPrecedence The null precedence to use
 *
 * @return {@code this}, for method chaining
 */
public Order nulls(NullPrecedence nullPrecedence) {
	this.nullPrecedence = nullPrecedence;
	return this;
}
 
Example 17
Source Project: lams   Source File: SessionFactoryBuilder.java    License: GNU General Public License v2.0 2 votes vote down vote up
/**
 * Apply a null precedence (NULLS FIRST, NULLS LAST) to be applied order-by clauses rendered into
 * SQL queries.
 *
 * @param nullPrecedence The default null precedence to use.
 *
 * @return {@code this}, for method chaining
 *
 * @see org.hibernate.cfg.AvailableSettings#DEFAULT_NULL_ORDERING
 */
SessionFactoryBuilder applyDefaultNullPrecedence(NullPrecedence nullPrecedence);
 
Example 18
NullPrecedence getDefaultNullPrecedence();