java Hibernate:在名为查询的存储过程中映射自定义列名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4863883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:22:54  来源:igfitidea点击:

Hibernate: Mapping custom column names in stored procedure named query

javahibernatestored-proceduresnamed-query

提问by limc

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_firstand name_lastare 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.

name_firstname_last是存储过程返回的确切列名。我创建了一个包含相同列名的 bean,以便我可以将查询结果映射到该 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:-

调用命名查询并将结果映射到 bean 的 Hibernate 代码:-

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:-

所有这些工作正常,但不是依赖于存储过程中的列名,我想在 中使用我自己的列名MyBean,例如:-

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.

更新- 我在下面添加了我的最终解决方案。

采纳答案by jpkrohling

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.

您需要实现自己的 ResultTransformer。这真的很简单,您可以查看捆绑实现的来源以获取灵感。

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

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

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

回答by limc

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

基于@partenon 对使用 custom 的回答ResultTransformer,这是最终的解决方案:-

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();

回答by JB Nizet

Just build your bean manually :

只需手动构建您的 bean:

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.

这还有一个额外的好处:它允许您使 MyBean 不可变。

回答by Luciano Fiandesio

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

您可以使用此博客文章中描述的解决方案。它工作得很好,可以很好地重复使用。