下面列出了怎么用org.hibernate.type.DateType的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* {@inheritDoc}
*/
@Override
public ProfileStatus getUserStatus(final String userId, final Date oldestDate) {
final HibernateCallback<ProfileStatus> hcb = session -> {
final Query q = session.getNamedQuery(QUERY_GET_USER_STATUS);
q.setParameter(USER_UUID, userId, StringType.INSTANCE);
q.setParameter(OLDEST_STATUS_DATE, oldestDate, DateType.INSTANCE);
q.setMaxResults(1);
return (ProfileStatus) q.uniqueResult();
};
return getHibernateTemplate().execute(hcb);
}
/**
* {@inheritDoc}
*/
@Override
public ProfileStatus getUserStatus(final String userId, final Date oldestDate) {
final HibernateCallback<ProfileStatus> hcb = session -> {
final Query q = session.getNamedQuery(QUERY_GET_USER_STATUS);
q.setParameter(USER_UUID, userId, StringType.INSTANCE);
q.setParameter(OLDEST_STATUS_DATE, oldestDate, DateType.INSTANCE);
q.setMaxResults(1);
return (ProfileStatus) q.uniqueResult();
};
return getHibernateTemplate().execute(hcb);
}
public void testCompositeIDQuery() throws Exception {
Session s = openSession();
Fum fee = new Fum( fumKey("fee",true) );
fee.setFum("fee");
s.save(fee);
Fum fi = new Fum( fumKey("fi",true) );
fi.setFum("fi");
short fiShort = fi.getId().getShort();
s.save(fi);
Fum fo = new Fum( fumKey("fo",true) );
fo.setFum("fo");
s.save(fo);
Fum fum = new Fum( fumKey("fum",true) );
fum.setFum("fum");
s.save(fum);
s.flush();
s.connection().commit();
s.close();
s = openSession();
// Try to find the Fum object "fo" that we inserted searching by the string in the id
List vList = s.find("from Fum fum where fum.id.string='fo'" );
assertTrue( "find by composite key query (find fo object)", vList.size() == 1 );
fum = (Fum)vList.get(0);
assertTrue( "find by composite key query (check fo object)", fum.getId().getString().equals("fo") );
// Try to find the Fum object "fi" that we inserted searching by the date in the id
vList = s.find("from Fum fum where fum.id.short = ?",new Short(fiShort),Hibernate.SHORT);
assertTrue( "find by composite key query (find fi object)", vList.size() == 1 );
fi = (Fum)vList.get(0);
assertTrue( "find by composite key query (check fi object)", fi.getId().getString().equals("fi") );
// Make sure we can return all of the objects by searching by the date id
assertTrue(
"find by composite key query with arguments",
s.find("from Fum fum where fum.id.date <= ? and not fum.fum='FRIEND'",new Date(),Hibernate.DATE).size()==4
);
s.flush();
s.connection().commit();
s.close();
s = openSession();
assertTrue(
s.iterate("select fum.id.short, fum.id.date, fum.id.string from Fum fum").hasNext()
);
assertTrue(
s.iterate("select fum.id from Fum fum").hasNext()
);
Query qu = s.createQuery("select fum.fum, fum , fum.fum, fum.id.date from Fum fum");
Type[] types = qu.getReturnTypes();
assertTrue(types.length==4);
for ( int k=0; k<types.length; k++) {
assertTrue( types[k]!=null );
}
assertTrue(types[0] instanceof StringType);
assertTrue(types[1] instanceof EntityType);
assertTrue(types[2] instanceof StringType);
assertTrue(types[3] instanceof DateType);
Iterator iter = qu.iterate();
int j = 0;
while ( iter.hasNext() ) {
j++;
assertTrue( ( (Object[]) iter.next() )[1] instanceof Fum );
}
assertTrue( "iterate on composite key", j==8 );
fum = (Fum) s.load( Fum.class, fum.getId() );
s.filter( fum.getQuxArray(), "where this.foo is null" );
s.filter( fum.getQuxArray(), "where this.foo.id = ?", "fooid", Hibernate.STRING );
Query f = s.createFilter( fum.getQuxArray(), "where this.foo.id = :fooId" );
f.setString("fooId", "abc");
assertFalse( f.iterate().hasNext() );
iter = s.iterate("from Fum fum where not fum.fum='FRIEND'");
int i = 0;
while ( iter.hasNext() ) {
fum = (Fum) iter.next();
//iter.remove();
s.delete(fum);
i++;
}
assertTrue( "iterate on composite key", i==4 );
s.flush();
s.iterate("from Fum fu, Fum fo where fu.fo.id.string = fo.id.string and fo.fum is not null");
s.find("from Fumm f1 inner join f1.fum f2");
s.connection().commit();
s.close();
}
@Override
public final DateType getHibernateType() {
return DateType.INSTANCE;
}
/**
* Bind a positional Date-valued parameter using just the Date portion.
*
* @param position The parameter position
* @param val The bind value
*
* @return {@code this}, for method chaining
*
* @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)}
* instead
*/
@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setDate(int position, Date val) {
setParameter( position, val, DateType.INSTANCE );
return this;
}
/**
* Bind a positional Calendar-valued parameter using just the Date portion.
*
* @param position The parameter position
* @param val The bind value
*
* @return {@code this}, for method chaining
*
* @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)}
* instead
*/
@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setCalendarDate(int position, Calendar val) {
setParameter( position, val, DateType.INSTANCE );
return this;
}
/**
* Bind the val (time is truncated) of a given Date object to a named query parameter.
*
* @param name The name of the parameter
* @param val The val object
*
* @return {@code this}, for method chaining
*
* @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)}
* instead
*/
@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setDate(String name, Date val) {
setParameter( name, val, DateType.INSTANCE );
return this;
}
/**
* Bind a named Calendar-valued parameter using just the Date portion.
*
* @param name The parameter name
* @param value The bind value
*
* @return {@code this}, for method chaining
*
* @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)}
* instead
*/
@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setCalendarDate(String name, Calendar value) {
setParameter( name, value, DateType.INSTANCE );
return this;
}
/**
* Bind a positional Date-valued parameter using just the Date portion.
*
* @param position The parameter position
* @param val The bind value
*
* @return {@code this}, for method chaining
*
* @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)}
* instead
*/
@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setDate(int position, Date val) {
setParameter( position, val, DateType.INSTANCE );
return this;
}
/**
* Bind a positional Calendar-valued parameter using just the Date portion.
*
* @param position The parameter position
* @param val The bind value
*
* @return {@code this}, for method chaining
*
* @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)}
* instead
*/
@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setCalendarDate(int position, Calendar val) {
setParameter( position, val, DateType.INSTANCE );
return this;
}
/**
* Bind the val (time is truncated) of a given Date object to a named query parameter.
*
* @param name The name of the parameter
* @param val The val object
*
* @return {@code this}, for method chaining
*
* @deprecated (since 5.2) use {@link #setParameter(String, Object)} or {@link #setParameter(String, Object, Type)}
* instead
*/
@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setDate(String name, Date val) {
setParameter( name, val, DateType.INSTANCE );
return this;
}
/**
* Bind a named Calendar-valued parameter using just the Date portion.
*
* @param name The parameter name
* @param value The bind value
*
* @return {@code this}, for method chaining
*
* @deprecated (since 5.2) use {@link #setParameter(String, Object)} or {@link #setParameter(String, Object, Type)}
* instead
*/
@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setCalendarDate(String name, Calendar value) {
setParameter( name, value, DateType.INSTANCE );
return this;
}