javax.persistence.Parameter Java Examples

The following examples show how to use javax.persistence.Parameter. 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 File: ReactiveQueryImpl.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public <P> ReactiveQueryImpl<R> setParameter(Parameter<P> parameter, P value) {
	if (explicitParameterInfoMap==null) {
		// not a criteria query
		super.setParameter( parameter, value );
	}
	else {
		final ExplicitParameterInfo<?> parameterInfo = resolveParameterInfo( parameter );
		if ( parameterInfo.isNamed() ) {
			setParameter( parameterInfo.getName(), value );
		}
		else {
			setParameter( parameterInfo.getPosition(), value );
		}
	}
	return this;
}
 
Example #2
Source File: FilterTest.java    From activejpa with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSetParameters() {
	Filter filter = new Filter()
			.condition("testKey", Operator.eq, "testValue")
			.condition("testKey1", Operator.eq, "testValue1");
	Path path = mock(Path.class);
	when(path.getJavaType()).thenReturn(String.class);
	filter.getConditions().get(0).setPath(path);
	filter.getConditions().get(1).setPath(path);
	Query query = mock(Query.class);
	Parameter param = mock(Parameter.class);
	when(param.getParameterType()).thenReturn(String.class);
	when(query.getParameter(anyString())).thenReturn(param);
	filter.setParameters(query);
	verify(query).setParameter("testKey", "testValue");
	verify(query).setParameter("testKey1", "testValue1");
}
 
Example #3
Source File: DailySalesGenerator.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
private void generate(Date date){
    Parameter<Date> p1 = baseQuery.getParameter("date1", Date.class);
    baseQuery.setParameter(p1, date);
    List<Object[]> results = baseQuery.getResultList();
    for (Object[] result : results){
        DailySales hourlySales = new DailySales();
        hourlySales.setRegionId((Region)result[0]);
        //hourlySales.setEmployeeId(null);
        //hourlySales.setDealerId(null);
        hourlySales.setProductId((Product)result[1]);
        hourlySales.setStateProvCd((String)result[2]);
        hourlySales.setQuantity(((Long)result[3]).intValue());
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(Calendar.YEAR, (Integer) result[4]);
        cal.set(Calendar.MONTH, (Integer) result[5]-1);
        cal.set(Calendar.DAY_OF_MONTH, (Integer) result[6]);
        hourlySales.setDate(cal.getTime());
        em.persist(hourlySales);
    }
    removeStaleEntries(date);
    
    
    
}
 
Example #4
Source File: JPA.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
public static void bind(Query query, Map<String, ?> namedArgs, Object... args) {
	if (args != null) {
		for (int i = 0; i < args.length; i++) {
			query.setParameter(i + 1, args[i]);
		}
	}

	if (namedArgs != null) {
		for (Parameter<?> param : query.getParameters()) {
			String name = param.getName();
			if (U.notEmpty(name)) {
				U.must(namedArgs.containsKey(name), "A named argument wasn't specified for the named JPQL parameter: %s", name);
				query.setParameter(name, Cls.convert(namedArgs.get(name), param.getParameterType()));
			}
		}
	}
}
 
Example #5
Source File: AbstractProducedQuery.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public <T> T getParameterValue(Parameter<T> parameter) {
	LOGGER.tracef( "#getParameterValue(%s)", parameter );

	getProducer().checkOpen( false );

	if ( !getParameterMetadata().containsReference( (QueryParameter) parameter ) ) {
		throw new IllegalArgumentException( "Parameter reference [" + parameter + "] did not come from this query" );
	}

	final QueryParameterBinding<T> binding = getQueryParameterBindings().getBinding( (QueryParameter<T>) parameter );
	LOGGER.debugf( "Checking whether parameter reference [%s] is bound : %s", parameter, binding.isBound() );
	if ( !binding.isBound() ) {
		throw new IllegalStateException( "Parameter value not yet bound : " + parameter.toString() );
	}
	return binding.getBindValue();
}
 
Example #6
Source File: CriteriaQueryTypeQueryAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public QueryImplementor<X> setParameter(Parameter<LocalDateTime> param, LocalDateTime value, TemporalType temporalType){
	entityManager.checkOpen( false );
	final ExplicitParameterInfo<?> parameterInfo = resolveParameterInfo( param );
	if ( parameterInfo.isNamed() ) {
		jpqlQuery.setParameter( parameterInfo.getName(), value, temporalType );
	}
	else {
		jpqlQuery.setParameter( parameterInfo.getPosition(), value, temporalType );
	}
	return this;
}
 
Example #7
Source File: ProcedureParameterMetadata.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Set<Parameter<?>> collectAllParametersJpa() {
	final Set<Parameter<?>> rtn = new LinkedHashSet<>();
	for ( ProcedureParameter parameter : parameters ) {
		rtn.add( parameter );
	}
	return rtn;
}
 
Example #8
Source File: LiveSalesListFacadeREST.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
@GET
@Path("/date/{from}")
@Produces({"application/xml", "application/json"})
public List<LiveSalesList> findFrom(@PathParam("from") Integer from) {
    Query q = getEntityManager().createNamedQuery("LiveSalesList.findFromOrderLineId");
    Parameter<Integer> p = q.getParameter("orderLineId", Integer.class);
    q.setParameter(p, from);
    return q.getResultList();
}
 
Example #9
Source File: CriteriaQueryTypeQueryAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isBound(Parameter<?> param) {
	entityManager.checkOpen( false );
	final ExplicitParameterInfo<?> parameterInfo = resolveParameterInfo( param );
	Parameter<?> jpqlParameter;
	if ( parameterInfo.isNamed() ) {
		jpqlParameter = jpqlQuery.getParameter( parameterInfo.getName() );
	}
	else {
		jpqlParameter = jpqlQuery.getParameter( parameterInfo.getPosition() );
	}
	return jpqlQuery.isBound( jpqlParameter );
}
 
Example #10
Source File: JtaQuery.java    From tomee with Apache License 2.0 5 votes vote down vote up
public <T> Query setParameter(final Parameter<T> param, final T value) {
    if (!underTx) {
        appliedOperations.add(new QueryOperation() {
            @Override
            public Query apply(final Query query) {
                return query.setParameter(param, value);
            }
        });
    }
    return query.setParameter(param, value);
}
 
Example #11
Source File: CumulativeLiveSalesFacadeREST.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
@GET
@Path("/state/{from}/{to}/{regionId}")
@Produces({"application/xml", "application/json"})
public List<StateTransitCumulativeSales> findStateRange(@PathParam("from") String from, @PathParam("to") String to, @PathParam("regionId") Integer regionId) {
    System.out.println("START findRegionRange (from="+from+" , to="+to+")");
    long DIFF, TIME = System.currentTimeMillis(), START_TIME = System.currentTimeMillis();
    Query q = getEntityManager().createQuery(STATE_SUM_QUERY);
    Parameter<Integer> p1 = q.getParameter("startId", Integer.class);
    q.setParameter(p1, Integer.parseInt(from));
    Parameter<Integer> p2 = q.getParameter("endId", Integer.class);
    q.setParameter(p2, Integer.parseInt(to));
    Parameter<Integer> p3 = q.getParameter("regionId", Integer.class);
    q.setParameter(p3, regionId);
    
    List<StateTransitCumulativeSales> result = new ArrayList<StateTransitCumulativeSales>();
    List<Object[]> resultList = q.getResultList();
    
    
    for (int i=0; i < resultList.size(); i++){
        Object o[] = resultList.get(i);
        StateTransitCumulativeSales t = new StateTransitCumulativeSales();
        t.setCost((Double)o[0]);
        t.setSales((Double) o[1]);
        t.setUnits((Long) o[2]);
        t.setState((String) o[3]);
        result.add(t);
    }
    
    DIFF = (System.currentTimeMillis() - START_TIME);
    System.out.println("    TOTAL TIME = "+DIFF+"ms");
    
    return result;
}
 
Example #12
Source File: RegionFacadeREST.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
@GET
@Path("/international/{international}")
@Produces({"application/xml", "application/json"})
public List<Region> findInternational(@PathParam("international")
Integer international) {
    Query q = getEntityManager().createNamedQuery("Region.findByInternational");
    Parameter<Integer> p = q.getParameter("international", Integer.class);
    q.setParameter(p, international);
    return q.getResultList();
}
 
Example #13
Source File: CriteriaQueryTypeQueryAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T> QueryImplementor<X> setParameter(Parameter<T> param, T t) {
	entityManager.checkOpen( false );
	final ExplicitParameterInfo<?> parameterInfo = resolveParameterInfo( param );
	if ( parameterInfo.isNamed() ) {
		jpqlQuery.setParameter( parameterInfo.getName(), t );
	}
	else {
		jpqlQuery.setParameter( parameterInfo.getPosition(), t );
	}
	return this;
}
 
Example #14
Source File: AbstractProducedQuery.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private <P> void setParameter(Parameter<P> parameter, Object value, Type type) {
	if ( parameter instanceof QueryParameter ) {
		setParameter( (QueryParameter) parameter, value, type );
	}
	else if ( value == null ) {
		locateBinding( parameter ).setBindValue( null, type );
	}
	else if ( value instanceof Collection && !isRegisteredAsBasicType( value.getClass() ) ) {
		locateListBinding( parameter ).setBindValues( (Collection) value, type );
	}
	else {
		locateBinding( parameter ).setBindValue( (P) value, type );
	}
}
 
Example #15
Source File: AbstractProducedQuery.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType) {
	getProducer().checkOpen();
	getQueryParameterBindings().getBinding( (QueryParameter) param ).setBindValue( value, temporalType );
	return this;
}
 
Example #16
Source File: CriteriaQueryTypeQueryAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings({ "unchecked" })
public <T> Parameter<T> getParameter(String name, Class<T> type) {
	entityManager.checkOpen( false );
	Parameter parameter = locateParameterByName( name );
	if ( type.isAssignableFrom( parameter.getParameterType() ) ) {
		return parameter;
	}
	throw new IllegalArgumentException(
			"Named parameter [" + name + "] type is not assignanle to request type ["
					+ type.getName() + "]"
	);
}
 
Example #17
Source File: ActionExecute.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private Object script(EffectivePerson effectivePerson, Business business, Statement statement, Runtime runtime)
		throws Exception {
	Object data = null;

	ScriptContext scriptContext = this.scriptContext(effectivePerson, business, runtime);

	ScriptFactory.initialServiceScriptText().eval(scriptContext);

	CompiledScript compiledScript = this.getCompiledScriptOfScriptText(statement);
	Object o = compiledScript.eval(scriptContext);
	String text = ScriptFactory.asString(o);
	Class<? extends JpaObject> cls = this.clazz(business, statement);
	EntityManager em = business.entityManagerContainer().get(cls);
	Query query = em.createQuery(text);
	for (Parameter<?> p : query.getParameters()) {
		if (runtime.hasParameter(p.getName())) {
			query.setParameter(p.getName(), runtime.getParameter(p.getName()));
		}
	}
	query.setFirstResult((runtime.page - 1) * runtime.size);
	query.setMaxResults(runtime.size);
	if (StringUtils.equalsIgnoreCase(statement.getType(), Statement.TYPE_SELECT)) {
		data = query.getResultList();
	} else {
		business.entityManagerContainer().beginTransaction(cls);
		data = query.executeUpdate();
		business.entityManagerContainer().commit();
	}
	return data;
}
 
Example #18
Source File: DailySalesHeatMapFacadeREST.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private HashMap<String, Long> runBaseQuery(Date date){
    Calendar cal = Calendar.getInstance();
    long DIFF, TIME = System.currentTimeMillis(), START_TIME = System.currentTimeMillis();
    Query baseQuery = em.createQuery(BASE_QUERY);
    HashMap<String, Long> result = new HashMap<String, Long>();
    {
        cal.setTime(date);
        int dayMin = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        int dayMax = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, dayMin);
        Parameter<Date> p1 = baseQuery.getParameter("oldStartDate", Date.class);
        baseQuery.setParameter(p1, cal.getTime());
        cal.set(Calendar.DAY_OF_MONTH, dayMax);
        Parameter<Date> p2 = baseQuery.getParameter("oldEndDate", Date.class);
        baseQuery.setParameter(p2, cal.getTime());

        List<Object[]> resultList = baseQuery.getResultList();

        DIFF = (System.currentTimeMillis() - TIME);
        System.out.println("    Q TIME = "+DIFF+"ms");

        for (int i=0; i < resultList.size(); i++){
            Object o[] = resultList.get(i);
            result.put((String)o[1],(Long)o[0]);
        }
    }
    return result;
}
 
Example #19
Source File: CriteriaQueryTypeQueryAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public QueryImplementor<X> setParameter(Parameter<Instant> param, Instant value, TemporalType temporalType){
	entityManager.checkOpen( false );
	final ExplicitParameterInfo<?> parameterInfo = resolveParameterInfo( param );
	if ( parameterInfo.isNamed() ) {
		jpqlQuery.setParameter( parameterInfo.getName(), value, temporalType );
	}
	else {
		jpqlQuery.setParameter( parameterInfo.getPosition(), value, temporalType );
	}
	return this;
}
 
Example #20
Source File: JtaQuery.java    From tomee with Apache License 2.0 5 votes vote down vote up
public Query setParameter(final Parameter<Date> param, final Date value, final TemporalType temporalType) {
    if (!underTx) {
        appliedOperations.add(new QueryOperation() {
            @Override
            public Query apply(final Query query) {
                return query.setParameter(param, value, temporalType);
            }
        });
    }
    return query.setParameter(param, value, temporalType);
}
 
Example #21
Source File: AbstractProducedQuery.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Set<Parameter<?>> getParameters() {
	getProducer().checkOpen( false );
	return getParameterMetadata().collectAllParametersJpa();
}
 
Example #22
Source File: CriteriaQueryTypeQueryAdapter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
@SuppressWarnings({ "unchecked" })
public Set<Parameter<?>> getParameters() {
	entityManager.checkOpen( false );
	return new HashSet( explicitParameterInfoMap.values() );
}
 
Example #23
Source File: QueryStub.java    From development with Apache License 2.0 4 votes vote down vote up
@Override
public <T> Parameter<T> getParameter(String arg0, Class<T> arg1) {

    return null;
}
 
Example #24
Source File: CriteriaLogQuery.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public <T> Parameter<T> getParameter(final String name, final Class<T> type) {
    return delegate.getParameter(name, type);
}
 
Example #25
Source File: CriteriaLogQuery.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public Parameter<?> getParameter(final String name) {
    return delegate.getParameter(name);
}
 
Example #26
Source File: Query.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
Query<R> setParameter(Parameter<Date> param, Date value, TemporalType temporalType);
 
Example #27
Source File: Query.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
Query<R> setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType);
 
Example #28
Source File: JtaQuery.java    From tomee with Apache License 2.0 4 votes vote down vote up
public Parameter<?> getParameter(final int position) {
    return query.getParameter(position);
}
 
Example #29
Source File: JtaQuery.java    From tomee with Apache License 2.0 4 votes vote down vote up
public <T> Parameter<T> getParameter(final String name, final Class<T> type) {
    return query.getParameter(name, type);
}
 
Example #30
Source File: CriteriaQueryTypeQueryAdapter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public Parameter<?> getParameter(String name) {
	entityManager.checkOpen( false );
	return locateParameterByName( name );
}