All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.fluent.hibernate.builder.InBuilder Maven / Gradle / Ivy

Go to download

A library to work with Hibernate by fluent API. This library hasn't dependencies except Hibernate dependencies. It requires Java 1.6 and above.

There is a newer version: 0.3.1
Show newest version
package com.github.fluent.hibernate.builder;

import java.util.Collection;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.github.fluent.hibernate.internal.util.InternalUtils.CollectionUtils;
import com.github.fluent.hibernate.request.HibernateRequest;

/**
 *
 * @author alexey.pchelnikov.
 */
public class InBuilder implements IBuilder {

    private final String propertyName;

    private final Collection values;

    private boolean nothingForEmptyCollection;

    /*package*/InBuilder(String propertyName, Collection values) {
        this.propertyName = propertyName;
        this.values = values;
    }

    public IBuilder nothingForEmptyCollection() {
        nothingForEmptyCollection = true;
        return this;
    }

    @Override
    public  void build(HibernateRequest hibernateRequest) {
        if (nothingForEmptyCollection && CollectionUtils.isEmpty(values)) {
            hibernateRequest.add(getFalseRestriction());
        } else {
            hibernateRequest.in(propertyName, values);
        }
    }

    private Criterion getFalseRestriction() {
        return Restrictions.sqlRestriction("1<>1");
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy