在一个项目中,我是一名顾问,我遇到了这个问题。查看解决方案:
首先,我创建了以下方法:
protected <T> MyTypedQuery<T> createNamedAliasQuery(final String queryName, final Class<T> type) {
final Query q = getSafeEntityManager().createNamedQuery(queryName);
q.unwrap(org.hibernate.Query.class)
.setResultTransformer(Transformers.aliasToBean(type));
return new MyTypedQuery<T>(q);
}
其次,我创建了一个 MyTypedQuery,它看起来像一个包装器,如下所示:
public class MyTypedQuery<R> implements TypedQuery<R> {
private Query q;
public MyTypedQuery(Query q) {
this.q = q;
}
@Override
public int executeUpdate() {
return this.q.executeUpdate();
}
@Override
public int getFirstResult() {
return this.q.getFirstResult();
}
@Override
public FlushModeType getFlushMode() {
return this.q.getFlushMode();
}
@Override
public Map<String, Object> getHints() {
return this.q.getHints();
}
@Override
public LockModeType getLockMode() {
return this.q.getLockMode();
}
@Override
public int getMaxResults() {
return this.q.getMaxResults();
}
@Override
public Parameter<?> getParameter(String arg0) {
return this.q.getParameter(arg0);
}
@Override
public Parameter<?> getParameter(int arg0) {
return this.q.getParameter(arg0);
}
@SuppressWarnings("unchecked")
@Override
public <T> Parameter<T> getParameter(String arg0, Class<T> arg1) {
return (Parameter<T>) this.q.getParameter(arg0);
}
@Override
public <T> Parameter<T> getParameter(int arg0, Class<T> arg1) {
return (Parameter<T>) this.q.getParameter(arg0, arg1);
}
@Override
public <T> T getParameterValue(Parameter<T> arg0) {
return (T) this.q.getParameterValue(arg0);
}
@Override
public Object getParameterValue(String arg0) {
return this.q.getParameterValue(arg0);
}
@Override
public Object getParameterValue(int arg0) {
return this.q.getParameterValue(arg0);
}
@Override
public Set<Parameter<?>> getParameters() {
return this.q.getParameters();
}
@Override
public boolean isBound(Parameter<?> arg0) {
return this.q.isBound(arg0);
}
@Override
public <T> T unwrap(Class<T> arg0) {
return this.q.unwrap(arg0);
}
@SuppressWarnings("unchecked")
@Override
public List<R> getResultList() {
return (List<R>) this.q.getResultList();
}
@SuppressWarnings("unchecked")
@Override
public R getSingleResult() {
return (R) this.q.getSingleResult();
}
@Override
public TypedQuery<R> setFirstResult(int arg0) {
this.q.setFirstResult(arg0);
return this;
}
@Override
public TypedQuery<R> setFlushMode(FlushModeType arg0) {
this.q.setFlushMode(arg0);
return this;
}
@Override
public TypedQuery<R> setHint(String arg0, Object arg1) {
this.q.setHint(arg0, arg1);
return this;
}
@Override
public TypedQuery<R> setLockMode(LockModeType arg0) {
this.q.setLockMode(arg0);
return this;
}
@Override
public TypedQuery<R> setMaxResults(int arg0) {
this.q.setMaxResults(arg0);
return this;
}
@Override
public <T> TypedQuery<R> setParameter(Parameter<T> arg0, T arg1) {
this.q.setParameter(arg0, arg1);
return this;
}
@Override
public TypedQuery<R> setParameter(String arg0, Object arg1) {
this.q.setParameter(arg0, arg1);
return this;
}
@Override
public TypedQuery<R> setParameter(int arg0, Object arg1) {
this.q.setParameter(arg0, arg1);
return this;
}
@Override
public TypedQuery<R> setParameter(Parameter<Calendar> arg0, Calendar arg1, TemporalType arg2) {
this.q.setParameter(arg0, arg1, arg2);
return this;
}
@Override
public TypedQuery<R> setParameter(Parameter<Date> arg0, Date arg1, TemporalType arg2) {
this.q.setParameter(arg0, arg1, arg2);
return this;
}
@Override
public TypedQuery<R> setParameter(String arg0, Calendar arg1, TemporalType arg2) {
this.q.setParameter(arg0, arg1, arg2);
return this;
}
@Override
public TypedQuery<R> setParameter(String arg0, Date arg1, TemporalType arg2) {
this.q.setParameter(arg0, arg1, arg2);
return this;
}
@Override
public TypedQuery<R> setParameter(int arg0, Calendar arg1, TemporalType arg2) {
this.q.setParameter(arg0, arg1, arg2);
return this;
}
@Override
public TypedQuery<R> setParameter(int arg0, Date arg1, TemporalType arg2) {
this.q.setParameter(arg0, arg1, arg2);
return this;
}
}
第三(也是最后一个),使用很简单,例如:
final List<Car> list =
createNamedAliasQuery("your-named-query", Car.class)
.setParameter("idCar", idCar)
.setParameter("idModel", idModel)
.getResultList();
请注意,@SuppressWarnings("unchecked") 在我们的 MyTypedQuery 中出现一次,而不是在每次使用中。