<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    菜園子

    BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
      7 Posts :: 1 Stories :: 31 Comments :: 0 Trackbacks

    關(guān)于Hibernate的查詢從數(shù)據(jù)庫映射到JavaBean

     


     

    Hibernate除了HQL外,還支持SQL的查詢,API為createSQLQuery(sql),如果數(shù)據(jù)庫使用的是Oracle,
    由于數(shù)據(jù)庫表中的列都是大寫,所以在從resultset到j(luò)avabean的時(shí)候,需要完全匹配。

    一般我們會(huì)用DTO或者作為DTO的Entity,無論是采用addEntity(Class class)還是

    setResultTransformer(new AliasToBeanResultTransformer (CatDTO.class))

    都會(huì)遇到數(shù)據(jù)庫字段到Java的大小寫映射的問題,如果數(shù)據(jù)庫字段是小寫的id, 數(shù)據(jù)庫里面是大寫的ID,
    則會(huì)遇到
    org.hibernate.PropertyNotFoundException: Could not find setter for ID on class com....的問題。

    通過源碼發(fā)現(xiàn),要求java的屬性要和數(shù)據(jù)庫的字段名大小寫一樣,并且全匹配。

     

    這個(gè)可以通過模仿Spring的類似查詢解決。Spring的NamedJdbcTemplate有如下方法:namedJdbcTemplate.query(sql, params, new BeanPropertyRowMapper(clazz))

     

    也是通過執(zhí)行sql并把返回的結(jié)果轉(zhuǎn)換成Java的方法,這個(gè)就可以忽視數(shù)據(jù)庫字段的大小寫問題,
    仿照這個(gè)BeanPropertyRowMapper寫一個(gè)適用于Hibernate的Transformer。

    代碼如下:

    public class BeanTransformerAdapter<Timplements 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> mappedClassboolean 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(underscoredNamepd);

                    }

                    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(ii + 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 rsint index, PropertyDescriptor pdthrows SQLException {

            return JdbcUtils.getResultSetValue(rsindexpd.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.lengthi++) {

                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;

        }

     

    使用方式如下:

    Query query = getSession().createSQLQuery(sql).setResultTransformer(new BeanTransformerAdapter(entityClass));

     

    就可以不用管Oracle字段的大寫問題了,會(huì)匹配到j(luò)ava的對(duì)應(yīng)字段。

     



    QQ:24889356
    posted on 2014-08-27 15:08 GhostZhang 閱讀(10117) 評(píng)論(2)  編輯  收藏

    Feedback

    # re: 關(guān)于Hibernate的查詢從數(shù)據(jù)庫映射到JavaBean 2014-08-28 13:36 簡歷網(wǎng)
    謝謝博主的分享  回復(fù)  更多評(píng)論
      

    # re: 關(guān)于Hibernate的查詢從數(shù)據(jù)庫映射到JavaBean 2014-09-02 09:23 金利鎖業(yè)
    支持博主分享  回復(fù)  更多評(píng)論
      


    只有注冊用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲春色另类小说| 在线亚洲人成电影网站色www| 亚洲美女一区二区三区| 91视频精品全国免费观看| 永久亚洲成a人片777777 | 亚洲国产精品ⅴa在线观看| 免费精品国偷自产在线在线| 亚洲精品91在线| 免费看美女裸露无档网站| 亚洲性色精品一区二区在线| 天天摸天天操免费播放小视频| 亚洲AV噜噜一区二区三区| 免费成人午夜视频| 一个人免费播放在线视频看片| 亚洲中文字幕丝袜制服一区| 中文字幕在线成人免费看| 亚洲av无码国产精品色午夜字幕 | 成人免费视频小说| 国产精品亚洲专区一区| 亚洲日韩在线中文字幕第一页| 两个人www免费高清视频| 亚洲电影免费在线观看| 99久久免费国产精品特黄| 亚洲精华国产精华精华液好用| 国产免费观看网站| 两个人www免费高清视频| 亚洲成av人片不卡无码| 免费高清在线影片一区| 九九全国免费视频| 久久久亚洲欧洲日产国码二区| 99精品全国免费观看视频| 日韩在线视频免费 | 国产色在线|亚洲| 亚洲成av人片天堂网老年人| 免费看搞黄视频网站| 中文字幕亚洲综合久久综合| 亚洲综合无码AV一区二区| 日本一区二区三区免费高清| xxxxx做受大片视频免费| 亚洲国产成人无码av在线播放| 啊v在线免费观看|