Hibernate: Mapping custom column names in stored procedure named query

纵然是瞬间 提交于 2019-12-18 09:04:19

问题


I currently have the following named query that wraps around a stored procedure:-

<hibernate-mapping>
    <sql-query name="mySp">
        <return-scalar column="name_first" type="string" />
        <return-scalar column="name_last" type="string" />

        { call some_sp :param }
    </sql-query>
</hibernate-mapping>

The columns name_first and name_last are the exact column names returned by the stored procedure. I created a bean that contains the same column names so that I can map the queried result into that bean.

public class MyBean {
    private String  name_first;
    private String  name_last;  

    ...
}

The Hibernate code that calls the named query and map the result into the bean:-

MyBean myBean = (MyBean) sessionFactory.getCurrentSession()
                        .getNamedQuery("mySp")
                        .setParameter("param", param)
                        .setResultTransformer(Transformers.aliasToBean(MyBean.class))
                        .uniqueResult();

All of these work fine, but instead of relying on the column names from the stored procedure, I want to use my own column names in MyBean, for example:-

public class MyBean {
    private String  firstName; // instead of name_first
    private String  lastName;  // instead of name_last  

    ...
}

How do I map my column names against the stored procedure's columns in my named query above?

Thanks.

UPDATE - I added my final solution below.


回答1:


You'll need to implement your own ResultTransformer. It's really simple, and you can look at the source of the bundled implementations for inspiration.

http://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/transform/ResultTransformer.html

https://github.com/hibernate/hibernate-core/tree/master/hibernate-core/src/main/java/org/hibernate/transform




回答2:


Based on @partenon's answer on using a custom ResultTransformer, here's the final solution:-

MyBean myBean = (MyBean) sessionFactory.getCurrentSession()
                    .getNamedQuery("mySp")
                    .setParameter("param", param)
                    .setResultTransformer(new BasicTransformerAdapter() {
                        private static final long   serialVersionUID    = 1L;

                        @Override
                        public Object transformTuple(Object[] tuple, String[] aliases) {
                            String firstName = (String) tuple[0];
                            String lastName = (String) tuple[1];

                            return new MyBean(firstName, lastName);
                        }
                    })
                    .uniqueResult();



回答3:


Just build your bean manually :

Object[] columns = (Object[]) sessionFactory.getCurrentSession()
                        .getNamedQuery("mySp")
                        .setParameter("param", param)
                        .uniqueResult();
MyBean myBean = new MyBean((String) columns[0], (String) columns[1]);

This has one additional advantage : it allows you to make your MyBean immutable.




回答4:


You can use the solution described in this blog post. It works great and can be reused nicely.



来源:https://stackoverflow.com/questions/4863883/hibernate-mapping-custom-column-names-in-stored-procedure-named-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!