日期:2014-05-16 浏览次数:20539 次
/**
* @return 返回导出对象对应的类
* @throws Exception
* 抛出异常
*/
public Object get(String className) throws Exception {
Class<?> clz = Class.forName(className);
Object obj = clz.newInstance();
return obj;
}
/**
* @param obj 要转换的对象 这个对象此时是代理类
* @param session hibernate的session
*
*/
public Object ObjectByClassName(Object obj,Session session){
obj = (Object)((SessionImpl)session).immediateLoad(fieldName,session.getIdentifier(obj));
//通过session查询语句,得到一个完整的对象
return obj;
}
/**
*
* @param obj 动态获取的对象
* @param fieldName 该对象的类
*
* @return 导出对象中分类条件字段值
*/
private Object getProperty(Object obj,String fieldName) {
Object value = null;
Field property;
try {
Class<?> conClass = obj.getClass();
property = conClass.getDeclaredField(fieldName);
property.setAccessible(true); //如果要访问的属性是私有的,那么此句很必要,你懂的!
try {
value=property.get(obj);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}
return value;
}
/**
* @obj 对象
* @fieldName 属性名称
* @return 返回分类字段值的name属性的Field
* @throws Exception
*/
public Object getFieldObject(Object obj,String fieldName){
Class<?> conClass = obj.getClass();
try {
Field[] conField = conClass.getDeclaredFields();
int row = 10;//防止找不到此名称的属性造成无限循环
while(row>0){
for(int i=0;i<conField.length;i++){
Field field = conField[i];
field.setAccessible(true);
if(field.getName().equals(fieldName)){
try {
return field.get(obj);
} catch (IllegalArgumentException e) {
return "";
} catch (IllegalAccessException e) {
return "";
}
}
}
conClass =conClass.getSuperclass();
conField = conClass.getDeclaredFields();
row--;
}
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
return "";
}
return "";
}
}
//property是已获得的一个泛型的属性值
if (property.getClass().getName().endsWith(this.SET_TYPE)) {
PersistentSet sets = (PersistentSet)property;
Iterator<?> itSet = sets.iterator();
while(itSet.hasNext()){
Object it = itSet.next();//在这里hibernate执行了查询语句,得到了一个完整的对象
String setClassName = it.getClass();
}
}