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

com.mysema.query.jpa.JPAQueryBase Maven / Gradle / Ivy

There is a newer version: 3.7.4
Show newest version
/*
 * Copyright 2011, Mysema Ltd
 * 
 * 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.mysema.query.jpa;

import java.util.Map;

import javax.annotation.Nullable;
import javax.persistence.EntityManager;

import com.mysema.query.QueryMetadata;
import com.mysema.query.support.ProjectableQuery;
import com.mysema.query.types.CollectionExpression;
import com.mysema.query.types.EntityPath;
import com.mysema.query.types.MapExpression;
import com.mysema.query.types.Path;
import com.mysema.query.types.Predicate;
import com.mysema.query.types.template.NumberTemplate;

/**
 * JPAQueryBase is a base Query class for JPA queries
 *
 * @author tiwe
 */
public abstract class JPAQueryBase> extends ProjectableQuery implements JPQLQuery {

    private Map constants;

    protected final JPAQueryMixin queryMixin;

    private final JPQLTemplates templates;
    
    @Nullable
    protected final EntityManager entityManager;

    @SuppressWarnings("unchecked")
    public JPAQueryBase(QueryMetadata md, JPQLTemplates templates, @Nullable EntityManager entityManager) {
        super(new JPAQueryMixin(md));
        super.queryMixin.setSelf((Q) this);
        this.queryMixin = (JPAQueryMixin) super.queryMixin;
        this.templates = templates;
        this.entityManager = entityManager;
    }

    protected JPQLTemplates getTemplates() {
        return templates;
    }
    
    protected String buildQueryString(boolean forCountRow) {
        if (queryMixin.getMetadata().getJoins().isEmpty()) {
            throw new IllegalArgumentException("No joins given");
        }
        JPQLSerializer serializer = new JPQLSerializer(templates, entityManager);
        serializer.serialize(queryMixin.getMetadata(), forCountRow, null);
        constants = serializer.getConstantToLabel();
        return serializer.toString();
    }

    protected void reset() {
        queryMixin.getMetadata().reset();
    }

    @Override
    public boolean exists() {
        if (templates.isSelect1Supported()) {
            return limit(1).singleResult(NumberTemplate.ONE) != null;
        } else {            
            EntityPath entityPath = (EntityPath) queryMixin.getMetadata().getJoins().get(0).getTarget();
            return !limit(1).list(entityPath).isEmpty();
        }                
    }

    public Q fetch() {
        return queryMixin.fetch();
    }

    public Q fetchAll() {
        return queryMixin.fetchAll();
    }

    public Q from(EntityPath arg) {
        return queryMixin.from(arg);
    }
    
    public Q from(EntityPath... args) {
        return queryMixin.from(args);
    }

    public 

Q fullJoin(CollectionExpression target) { return queryMixin.fullJoin(target); } public

Q fullJoin(CollectionExpression target, Path

alias) { return queryMixin.fullJoin(target, alias); } public

Q fullJoin(EntityPath

target) { return queryMixin.fullJoin(target); } public

Q fullJoin(EntityPath

target, Path

alias) { return queryMixin.fullJoin(target, alias); } public

Q fullJoin(MapExpression target) { return queryMixin.fullJoin(target); } public

Q fullJoin(MapExpression target, Path

alias) { return queryMixin.fullJoin(target, alias); } protected Map getConstants() { return constants; } public

Q innerJoin(CollectionExpression target) { return queryMixin.innerJoin(target); } public

Q innerJoin(CollectionExpressiontarget, Path

alias) { return queryMixin.innerJoin(target, alias); } public

Q innerJoin(EntityPath

target) { return queryMixin.innerJoin(target); } public

Q innerJoin(EntityPath

target, Path

alias) { return queryMixin.innerJoin(target, alias); } public

Q innerJoin(MapExpression target) { return queryMixin.innerJoin(target); } public

Q innerJoin(MapExpression target, Path

alias) { return queryMixin.innerJoin(target, alias); } public

Q join(CollectionExpression target) { return queryMixin.join(target); } public

Q join(CollectionExpression target, Path

alias) { return queryMixin.join(target, alias); } public

Q join(EntityPath

target) { return queryMixin.join(target); } public

Q join(EntityPath

target, Path

alias) { return queryMixin.join(target, alias); } public

Q join(MapExpression target) { return queryMixin.join(target); } public

Q join(MapExpression target, Path

alias) { return queryMixin.join(target, alias); } public

Q leftJoin(CollectionExpression target) { return queryMixin.leftJoin(target); } public

Q leftJoin(CollectionExpression target, Path

alias) { return queryMixin.leftJoin(target, alias); } public

Q leftJoin(EntityPath

target) { return queryMixin.leftJoin(target); } public

Q leftJoin(EntityPath

target, Path

alias) { return queryMixin.leftJoin(target, alias); } public

Q leftJoin(MapExpression target) { return queryMixin.leftJoin(target); } public

Q leftJoin(MapExpression target, Path

alias) { return queryMixin.leftJoin(target, alias); } public

Q rightJoin(CollectionExpression target) { return queryMixin.rightJoin(target); } public

Q rightJoin(CollectionExpression target, Path

alias) { return queryMixin.rightJoin(target, alias); } public

Q rightJoin(EntityPath

target) { return queryMixin.rightJoin(target); } public

Q rightJoin(EntityPath

target, Path

alias) { return queryMixin.rightJoin(target, alias); } public

Q rightJoin(MapExpression target) { return queryMixin.rightJoin(target); } public

Q rightJoin(MapExpression target, Path

alias) { return queryMixin.rightJoin(target, alias); } public Q on(Predicate condition) { return queryMixin.on(condition); } public Q on(Predicate... conditions) { return queryMixin.on(conditions); } protected void setConstants(Map constants) { this.constants = constants; } protected String toCountRowsString() { return buildQueryString(true); } protected String toQueryString() { return buildQueryString(false); } @Override public String toString() { return buildQueryString(false).trim(); } public QueryMetadata getMetadata() { return queryMixin.getMetadata(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy