ibernate除了HQL外,还支持SQL的查询,API为createSQLQuery(sql),如果数据库使用的是Oracle,
由于数据库表中的列都是大写,所以在从resultset到javabean的时候,需要完全匹配。
一般我们会用DTO或者作为DTO的Entity,无论是采用addEntity(Class class)还是
setResultTransformer(new AliasToBeanResultTransformer (CatDTO.class))
都会遇到数据库字段到Java的大小写映射的问题,如果数据库字段是小写的id, 数据库里面是大写的ID,
则会遇到org.hibernate.PropertyNotFoundException: Could not find setter for ID on class com....的问题。
通过源码发现,要求java的属性要和数据库的字段名大小写一样,并且全匹配。
public class BeanTransformerAdapter<T> implements ResultTransformer { /** Logger available to subclasses */ protected final Log logger = LogFactory.getLog(getClass()); /** The class we are mapping to */ private Class<T> mappedClass; /** Whether we're strictly validating */ private boolean checkFullyPopulated = false; /** Whether we're defaulting primitives when mapping a null value */ private boolean primitivesDefaultedForNullValue = false; /** Map of the fields we provide mapping for */ private Map<String, PropertyDescriptor> mappedFields; /** Set of bean properties we provide mapping for */ private Set<String> mappedProperties; /** * Create a new BeanPropertyRowMapper for bean-style configuration. * @see #setMappedClass * @see #setCheckFullyPopulated */ public BeanTransformerAdapter() { } /** * Create a new BeanPropertyRowMapper, accepting unpopulated properties * in the target bean. * <p>Consider using the {@link #newInstance} factory method instead, * which allows for specifying the mapped type once only. * @param mappedClass the class that each row should be mapped to */ public BeanTransformerAdapter(Class<T> mappedClass) { initialize(mappedClass); } /** * Create a new BeanPropertyRowMapper. * @param mappedClass the class that each row should be mapped to * @param checkFullyPopulated whether we're strictly validating that * all bean properties have been mapped from corresponding database fields */ public BeanTransformerAdapter(Class<T> mappedClass, boolean checkFullyPopulated) { initialize(mappedClass); this.checkFullyPopulated = checkFullyPopulated; } /** * Set the class that each row should be mapped to. */ public void setMappedClass(Class<T> mappedClass) { if (this.mappedClass == null) { initialize(mappedClass); } else { if (!this.mappedClass.equals(mappedClass)) { throw new InvalidDataAccessApiUsageException("The mapped class can not be reassigned to map to " + mappedClass + " since it is already providing mapping for " + this.mappedClass); } } } /** * Initialize the mapping metadata for the given class. * @param mappedClass the mapped class. */ protected void initialize(Class<T> mappedClass) { this.mappedClass = mappedClass; this.mappedFields = new HashMap<String, PropertyDescriptor>(); this.mappedProperties = new HashSet<String>(); PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass); for (PropertyDescriptor pd : pds) { if (pd.getWriteMethod() != null) { this.mappedFields.put(pd.getName().toLowerCase(), pd); String underscoredName = underscoreName(pd.getName()); if (!pd.getName().toLowerCase().equals(underscoredName)) { this.mappedFields.put(underscoredName, pd); } this.mappedProperties.add(pd.getName()); } } } /** * Convert a name in camelCase to an underscored name in lower case. * Any upper case letters are converted to lower case with a preceding underscore. * @param name the string containing original name * @return the converted name */ private String underscoreName(String name) { if (!StringUtils.hasLength(name)) { return ""; } StringBuilder result = new StringBuilder(); result.append(name.substring(0, 1).toLowerCase()); for (int i = 1; i < name.length(); i++) { String s = name.substring(i, i + 1); String slc = s.toLowerCase(); if (!s.equals(slc)) { result.append("_").append(slc); } else { result.append(s); } } return result.toString(); } /** * Get the class that we are mapping to. */ public final Class<T> getMappedClass() { return this.mappedClass; } /** * Set whether we're strictly validating that all bean properties have been * mapped from corresponding database fields. * <p>Default is {@code false}, accepting unpopulated properties in the * target bean. */ public void setCheckFullyPopulated(boolean checkFullyPopulated) { this.checkFullyPopulated = checkFullyPopulated; } /** * Return whether we're strictly validating that all bean properties have been * mapped from corresponding database fields. */ public boolean isCheckFullyPopulated() { return this.checkFullyPopulated; } /** * Set whether we're defaulting Java primitives in the case of mapping a null value * from corresponding database fields. * <p>Default is {@code false}, throwing an exception when nulls are mapped to Java primitives. */ public void setPrimitivesDefaultedForNullValue(boolean primitivesDefaultedForNullValue) { this.primitivesDefaultedForNullValue = primitivesDefaultedForNullValue; } /** * Return whether we're defaulting Java primitives in the case of mapping a null value * from corresponding database fields. */ public boolean isPrimitivesDefaultedForNullValue() { return primitivesDefaultedForNullValue; } /** * Initialize the given BeanWrapper to be used for row mapping. * To be called for each row. * <p>The default implementation is empty. Can be overridden in subclasses. * @param bw the BeanWrapper to initialize */ protected void initBeanWrapper(BeanWrapper bw) { } /** * Retrieve a JDBC object value for the specified column. * <p>The default implementation calls * {@link JdbcUtils#getResultSetValue(java.sql.ResultSet, int, Class)}. * Subclasses may override this to check specific value types upfront, * or to post-process values return from {@code getResultSetValue}. * @param rs is the ResultSet holding the data * @param index is the column index * @param pd the bean property that each result object is expected to match * (or {@code null} if none specified) * @return the Object value * @throws SQLException in case of extraction failure * @see org.springframework.jdbc.support.JdbcUtils#getResultSetValue(java.sql.ResultSet, int, Class) */ protected Object getColumnValue(ResultSet rs, int index, PropertyDescriptor pd) throws SQLException { return JdbcUtils.getResultSetValue(rs, index, pd.getPropertyType()); } /** * Static factory method to create a new BeanPropertyRowMapper * (with the mapped class specified only once). * @param mappedClass the class that each row should be mapped to */ public static <T> BeanPropertyRowMapper<T> newInstance(Class<T> mappedClass) { BeanPropertyRowMapper<T> newInstance = new BeanPropertyRowMapper<T>(); newInstance.setMappedClass(mappedClass); return newInstance; } @Override public Object transformTuple(Object[] tuple, String[] aliases) { T mappedObject = BeanUtils.instantiate(this.mappedClass); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject); initBeanWrapper(bw); Set<String> populatedProperties = (isCheckFullyPopulated() ? new HashSet<String>() : null); for (int i = 0; i < aliases.length; i++) { String column = aliases[i]; PropertyDescriptor pd = this.mappedFields.get(column.replaceAll(" ", "").toLowerCase()); if (pd != null) { try { Object value = tuple[i]; try { bw.setPropertyValue(pd.getName(), value); } catch (TypeMismatchException e) { if (value == null && primitivesDefaultedForNullValue) { logger.debug("Intercepted TypeMismatchException for column " + column + " and column '" + column + "' with value " + value + " when setting property '" + pd.getName() + "' of type " + pd.getPropertyType() + " on object: " + mappedObject); } else { throw e; } } if (populatedProperties != null) { populatedProperties.add(pd.getName()); } } catch (NotWritablePropertyException ex) { throw new DataRetrievalFailureException("Unable to map column " + column + " to property " + pd.getName(), ex); } } } if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) { throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " + "necessary to populate object of class [" + this.mappedClass + "]: " + this.mappedProperties); } return mappedObject; } @Override public List transformList(List list) { return list; }
这个可以通过模仿Spring的类似查询解决。Spring的NamedJdbcTemplate有如下方法:namedJdbcTemplate.query(sql, params, new BeanPropertyRowMapper(clazz))
也是通过执行sql并把返回的结果转换成Java的方法,这个就可以忽视数据库字段的大小写问题,
仿照这个BeanPropertyRowMapper写一个适用于Hibernate的Transformer。
代码如下:
使用方式如下:
Query query = getSession().createSQLQuery(sql).setResultTransformer(new BeanTransformerAdapter(entityClass));
就可以不用管Oracle字段的大写问题了,会匹配到java的对应字段。
原文地址:http://www.blogjava.net/ghostzhang/archive/2014/08/27/417408.html