APISonar


javax.persistence.TemporalType.DATE

> javax > persistence > TemporalType > DATE
javax APIs persistence APIs TemporalType APIs DATE APIs

Example 1
public void shouldSetTemporalQueryParameterToDate() throws Exception {

		Method method = SampleRepository.class.getMethod("validWithDefaultTemporalTypeParameter", Date.class);
		JpaParameters parameters = new JpaParameters(method);
		Date date = new Date();

		Object[] values = { date };
		bind(method, parameters, values);

		verify(query).setParameter(eq(1), eq(date), eq(TemporalType.DATE));
	}
Example 2
public void testBindingNulls() {
		EntityManager em = getOrCreateEntityManager();
		em.getTransaction().begin();

		try {
			Query query = em.createQuery( "from Thing t where t.someDate = :aDate or t.someTime = :aTime or t.someTimestamp = :aTimestamp" );
			query.setParameter( "aDate", (Date) null, TemporalType.DATE );
			query.setParameter( "aTime", (Date) null, TemporalType.DATE );
			query.setParameter( "aTimestamp", (Date) null, TemporalType.DATE );
		}
		finally {
			em.getTransaction().rollback();
			em.close();
		}
	}
Example 3
public void testSqlDate() {
        GregorianCalendar cal = new GregorianCalendar(); 
        cal.set(1901, 11, 31, 23, 59, 59);
        cal.set(Calendar.MILLISECOND, 999);

        List result = createEntityManager().createQuery("SELECT OBJECT(o) FROM DateTime o WHERE o.date = :date").
            setParameter("date", cal.getTime(), TemporalType.DATE).
            getResultList();
        
        assertTrue("There should be one result", result.size() == 1);
    }
Example 4
public void test_jpql_api_parameter_example_2() {
		doInJPA( this::entityManagerFactory, entityManager -> {
			Date timestamp = new Date(  );
			//tag::jpql-api-parameter-example[]

			// For generic temporal field types (e.g. `java.util.Date`, `java.util.Calendar`)
			// we also need to provide the associated `TemporalType`
			Query query = entityManager.createQuery(
				"select p " +
				"from Person p " +
				"where p.createdOn > :timestamp" )
			.setParameter( "timestamp", timestamp, TemporalType.DATE );
			//end::jpql-api-parameter-example[]
		});
	}
Example 5
public Collection<Event> getEventsDuringPeriod(Date start, Date end, int startChunk, int chunkSize) {
		
		Query query = manager.createQuery("SELECT e FROM Event e WHERE e.timestamp BETWEEN :start AND :end");
			    
		query = query.setParameter("start", start, TemporalType.DATE);
		query = query.setParameter("end", end, TemporalType.DATE);
		query = query.setFirstResult(startChunk);
        query = query.setMaxResults(chunkSize);
		
		return query.getResultList();
	}
Example 6
public void testQuerySetDateParameterByName() {
		EntityManager em = getOrCreateEntityManager();
		Query query = em.createQuery( "from AnEntity where birthDay = :bday" );
		em.close();
		try {
			query.setParameter( "bday", new Date(), TemporalType.DATE );
			fail( "should have thrown IllegalStateException" );
		}
		catch (IllegalStateException ex) {
			// expected
		}
	}
Example 7
public String getDefaultDateFormat(TemporalType temporalType) {
        return temporalType == TemporalType.DATE
                ? messages.getMainMessage("dateFormat")
                : messages.getMainMessage("dateTimeFormat");
    }
Example 8
private void doTest(String property, Object obj) {
		doTest( property, obj, TemporalType.DATE );
		doTest( property, obj, TemporalType.TIMESTAMP );
	}
Example 9
protected TemporalType getTemporalType(MetaProperty metaProperty, Class javaType) {
        TemporalType temporalType = null;

        if (java.sql.Date.class.equals(javaType) || LocalDate.class.equals(javaType)) {
            temporalType = TemporalType.DATE;
        } else if (metaProperty.getAnnotations() != null) {
            temporalType = (TemporalType) metaProperty.getAnnotations().get(MetadataTools.TEMPORAL_ANN_NAME);
        }
        return temporalType;
    }
Example 10
public void testTemporalType() throws Exception {
        Date nextYear = DateUtils.addYears(AppBeans.get(TimeSource.class).currentTimestamp(), 1);
        LoadContext<User> loadContext = LoadContext.create(User.class).setQuery(
                LoadContext.createQuery("select u from sec$User u where u.createTs = :ts")
                        .setParameter("ts", nextYear, TemporalType.DATE));

        List<User> users = dataManager.loadList(loadContext);
        assertTrue(users.isEmpty());
    }
Example 11
public void setupDateFormat(DateField component, EntityValueSource valueSource) {
        MetaProperty metaProperty = valueSource.getMetaPropertyPath().getMetaProperty();
        Class javaType = metaProperty.getRange().asDatatype().getJavaClass();

        TemporalType temporalType = getTemporalType(metaProperty, javaType);

        component.setResolution(temporalType == TemporalType.DATE
                ? DateField.Resolution.DAY
                : DateField.Resolution.MIN);

        String formatStr = messageTools.getDefaultDateFormat(temporalType);
        component.setDateFormat(formatStr);
    }
Example 12
public static String formatParamValue(Param param, Object value) {
        Datatype datatype = Datatypes.get(param.getJavaClass());
        MetaProperty property = param.getProperty();
        if (property != null) {
            TemporalType tt = (TemporalType) property.getAnnotations().get("temporal");
            if (tt == TemporalType.DATE) {
                datatype = Datatypes.get(DateDatatype.NAME);
            }
        }
        if (datatype != null) {
            UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.class);
            return datatype.format(value, userSessionSource.getLocale());
        }
        return value.toString();
    }
Example 13
protected Object convertTemporalType(Object value, TemporalType type) {
        ConversionManager conversionManager = ((oracle.toplink.essentials.internal.sessions.AbstractSession)getEntityManager().getActiveSession()).getDatasourcePlatform().getConversionManager();
        if (type == TemporalType.TIME) {
            return conversionManager.convertObject(value, ClassConstants.TIME);
        } else if (type == TemporalType.TIMESTAMP) {
            return conversionManager.convertObject(value, ClassConstants.TIMESTAMP);
        } else if (type == TemporalType.DATE) {
            return conversionManager.convertObject(value, ClassConstants.SQLDATE);
        }
        return value;
    }