浏览 592 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2008-03-13
代码如下:
public static List findObjectByOther(Class theClass, Field field, Object value){
List objs = new ArrayList();
Field[] fields = null;
boolean isAField = false;
boolean isTheType = false;
if(theClass != null){
fields = theClass.getDeclaredFields();
}
if(field != null){
isTheType = field.getType().isInstance(value); //假如使用 value instanceof field.getTypr(),则提示field.getTypr() 不是一个class,找不到原因所在,请大伙指教.
}
if(fields != null){
for(int i=0; i<fields.length; i++){
if(field.equals(fields[i])){
isAField = true;
break;
}
}
}
if(isAField && isTheType){
String hql = "from " + theClass.getName() + " where " + field.getName() + "=:value";
try {
HibernateSessionFactory.currentSession().beginTransaction();
objs = HibernateSessionFactory.currentSession().createQuery(hql).setParameter("value", value).list();
} catch (HibernateException e) {
log.error(e);
} catch (SessionInitialException e) {
log.error(e);
}
}
return objs;
}
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2008-03-13
instanceof左边是对象,右边是类型。
field.getTypr()返回的是Class类型的对象。 |
|
| 返回顶楼 | |
|
最后更新时间:2008-07-24
倒是对你的查询感兴趣
|
|
| 返回顶楼 | |




