通过Hibernate的会话工厂SessionFactory可以得到持久化对象相关的元数据信息及配置属性文件信息,从而可以很方便的持久化数据对象做批量信息的变更扩展,具体工具类代码示例如下
import java.lang.reflect.InvocationTargetException;@b@import java.lang.reflect.Method;@b@import java.util.HashMap;@b@@b@import org.apache.log4j.Logger;@b@import org.hibernate.SessionFactory;@b@import org.hibernate.metadata.ClassMetadata;@b@import org.hibernate.persister.entity.SingleTableEntityPersister;@b@ @b@ @b@public class HibernateBeanUtil implements IHibernateBeanUtil {@b@ @b@ protected static final Logger log = Logger.getLogger(HibernateBeanUtil.class);@b@ @b@ private SessionFactory sessionFactory;@b@ @b@ /**@b@ * 获得实体类设置中 字段名对应的普通属性@b@ * @b@ * @param className @b@ * 类的全名@b@ * @return 实体类不包括主键的普通属性 HashMap, key 为对应字段名, value 为属性名 .@b@ */@b@ public HashMap<String,String> getProperties(String className) {@b@ @b@ HashMap<String,String> properties = new HashMap<String,String>();@b@ @b@ try {@b@ SingleTableEntityPersister classMetadata = (SingleTableEntityPersister) sessionFactory@b@ .getClassMetadata(className);@b@ String[] propertyNames = classMetadata.getPropertyNames();@b@ @b@ String[] columnNames;@b@ for (int i = 0; i < propertyNames.length; i++) {@b@@b@ columnNames = classMetadata@b@ .getPropertyColumnNames(propertyNames[i]);@b@@b@ if (columnNames != null && columnNames.length > 0@b@ && !"ID".equalsIgnoreCase(columnNames[0])) {@b@ properties.put(columnNames[0], propertyNames[i]);@b@ }@b@@b@ }@b@ } catch (Exception e) {@b@ log.error(e.toString());@b@ }@b@ return properties;@b@ @b@ }@b@ @b@ /**@b@ * 获得实体类的普通属性对应的字段@b@ * @b@ * @param className @b@ * 类的全名@b@ * @return 实体类不包括主键的普通属性 HashMap, key 为属性名, value 为对应字段名.@b@ */@b@ public HashMap<String,String> getColumnNames(String className) {@b@ @b@ HashMap<String,String> properties = new HashMap<String,String>();@b@ @b@ try {@b@ SingleTableEntityPersister classMetadata = (SingleTableEntityPersister) sessionFactory@b@ .getClassMetadata(className);@b@ String[] propertyNames = classMetadata.getPropertyNames();@b@ @b@ String[] columnNames;@b@ for (int i = 0; i < propertyNames.length; i++) {@b@@b@ columnNames = classMetadata@b@ .getPropertyColumnNames(propertyNames[i]);@b@@b@ if (columnNames != null && columnNames.length > 0@b@ && !"ID".equalsIgnoreCase(columnNames[0])) {@b@ properties.put(propertyNames[i],columnNames[0]);@b@ }@b@@b@ }@b@ } catch (Exception e) {@b@ log.error(e.toString());@b@ }@b@ return properties;@b@ @b@ }@b@ @b@ /**@b@ * 获得实体类设置中 字段名对应的普通属性类型@b@ * @b@ * @param className @b@ * 类的全名@b@ * @return 实体类不包括主键的普通属性 HashMap, key 为对应字段名, value 为属性类型 .@b@ */@b@ public HashMap<String, Class<?>> getColumnsType(String className) {@b@ @b@ HashMap<String,Class<?>> properties = new HashMap<String, Class<?>>();@b@ @b@ try {@b@ SingleTableEntityPersister classMetadata = (SingleTableEntityPersister) sessionFactory@b@ .getClassMetadata(className);@b@ String[] propertyNames = classMetadata.getPropertyNames();@b@ @b@ ClassMetadata clsData = classMetadata.getClassMetadata(); @b@ @b@ String[] columnNames;@b@ for (int i = 0; i < propertyNames.length; i++) {@b@@b@ columnNames = classMetadata@b@ .getPropertyColumnNames(propertyNames[i]);@b@@b@ if (columnNames != null && columnNames.length > 0@b@ && !"ID".equalsIgnoreCase(columnNames[0])) { @b@ properties.put(columnNames[0], clsData.getPropertyType(propertyNames[i]).getReturnedClass());@b@ }@b@@b@ }@b@ } catch (Exception e) {@b@ log.error(e.toString());@b@ }@b@ return properties;@b@ @b@ }@b@ @b@ /**@b@ * 获得实体类设置中 普通属性的类型@b@ * @b@ * @param className @b@ * 类的全名@b@ * @return 实体类不包括主键的普通属性 HashMap, key 为普通属性名, value 为属性类型 .@b@ */@b@ public HashMap<String, Class<?>> getPropertiesType(String className) {@b@ @b@ HashMap<String, Class<?>> properties = new HashMap<String, Class<?>>();@b@ @b@ try {@b@ SingleTableEntityPersister classMetadata = (SingleTableEntityPersister) sessionFactory@b@ .getClassMetadata(className);@b@ String[] propertyNames = classMetadata.getPropertyNames();@b@ @b@ ClassMetadata clsData = classMetadata.getClassMetadata(); @b@ @b@ String[] columnNames;@b@ for (int i = 0; i < propertyNames.length; i++) {@b@@b@ columnNames = classMetadata@b@ .getPropertyColumnNames(propertyNames[i]);@b@@b@ if (columnNames != null && columnNames.length > 0@b@ && !"ID".equalsIgnoreCase(columnNames[0])) {@b@ properties.put(propertyNames[i], clsData.getPropertyType(propertyNames[i]).getReturnedClass());@b@ }@b@@b@ }@b@ } catch (Exception e) {@b@ log.error(e.toString());@b@ }@b@ return properties;@b@ @b@ }@b@ @b@ /**@b@ * 获得实体类设置中 普通属性的类型@b@ * @b@ * @param className @b@ * 类的全名@b@ * @param className @b@ * 类的属性名@b@ * @return 实体类不包括主键的普通属性的类型 .@b@ */@b@ public Class<?> getPropertyTypeByPropertyName(String className, String propertyName) {@b@ @b@ Class<?> propertyType = null;@b@ @b@ try {@b@ SingleTableEntityPersister classMetadata = (SingleTableEntityPersister) sessionFactory@b@ .getClassMetadata(className);@b@ @b@ propertyType = classMetadata.getPropertyType(propertyName).getReturnedClass();@b@ @b@@b@ } catch (Exception e) {@b@ log.error(e.toString());@b@ }@b@ return propertyType;@b@ @b@ }@b@ @b@ /**@b@ * 动态执行实体类某个属性的 set 方法@b@ * @b@ * @param object @b@ * 实体类对象@b@ * @param property @b@ * 实体类的属性@b@ * @param PropertyType @b@ * 实体类的属性类型@b@ * @param value @b@ * 实体类的属性值@b@ * @return .@b@ * @throws InvocationTargetException @b@ * @throws IllegalAccessException @b@ * @throws IllegalArgumentException @b@ */@b@ public void invokeSetMethodByProperty(Object object, String property, Class<?> PropertyType , Object value) throws Exception {@b@ @b@ StringBuffer methodName = new StringBuffer();@b@ methodName.append("set").append(property.substring(0,1).toUpperCase()).append(property.substring(1));@b@ @b@ Method method = object.getClass().getDeclaredMethod(methodName.toString(), new Class[]{PropertyType});@b@ method.invoke(object, new Object[]{value});@b@ }@b@ @b@ /**@b@ * 动态执行实体类某个属性的 get 方法@b@ * @b@ * @param object @b@ * 实体类对象@b@ * @param property @b@ * 实体类的属性@b@ * @return get方法的返回值.@b@ * @throws InvocationTargetException @b@ * @throws IllegalAccessException @b@ * @throws IllegalArgumentException @b@ */@b@ public Object invokeGetMethodByProperty(Object object, String property) throws Exception {@b@ @b@ StringBuffer methodName = new StringBuffer();@b@ methodName.append("get").append(property.substring(0,1).toUpperCase()).append(property.substring(1));@b@ @b@ Method method = object.getClass().getDeclaredMethod(methodName.toString(), new Class[]{});@b@ return method.invoke(object, new Object[]{});@b@ @b@ }@b@ @b@ /**@b@ * 动态执行实体类某个字段名对应普通属性的 set 方法@b@ * @b@ * @param object @b@ * 实体类对象@b@ * @param columnName @b@ * 实体类的属性对应的字段名@b@ * @param value @b@ * 实体类的属性值@b@ * @return .@b@ * @throws InvocationTargetException @b@ * @throws IllegalAccessException @b@ * @throws IllegalArgumentException @b@ */@b@ public void invokeSetMethodByColumnName(Object object, String columnName, Object value) throws Exception {@b@ HashMap<String, String> propertiesMap = this.getProperties(object.getClass().getName());@b@@b@ this.invokeSetMethodByProperty(object, propertiesMap.get(columnName), @b@ this.getPropertyTypeByPropertyName(object.getClass().getName(), propertiesMap.get(columnName)), value);@b@@b@ }@b@ @b@ /**@b@ * 动态执行实体类某个字段名对应普通属性的 get 方法@b@ * @b@ * @param object @b@ * 实体类对象@b@ * @param columnName @b@ * 实体类的属性对应的字段名@b@ * @return get方法的返回值.@b@ * @throws InvocationTargetException @b@ * @throws IllegalAccessException @b@ * @throws IllegalArgumentException @b@ */@b@ public Object invokeGetMethodByColumnName(Object object, String columnName) throws Exception {@b@ HashMap<String, String> propertiesMap = this.getProperties(object.getClass().getName());@b@ @b@ return this.invokeGetMethodByProperty(object, propertiesMap.get(columnName));@b@@b@ }@b@@b@ /**@b@ * @param sessionFactory the sessionFactory to set@b@ */@b@ public void setSessionFactory(SessionFactory sessionFactory) {@b@ this.sessionFactory = sessionFactory;@b@ }@b@@b@}