Java Hibernate 的 Transformers.aliasToBean() 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2445092/
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-08-13 07:46:44  来源:igfitidea点击:

Hibernate's Transformers.aliasToBean() method

javahibernate

提问by cometta

 Query query =  getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(
                 "select proj_employee.employee_no as employeeNo, ...
 .setResultTransformer(Transformers.aliasToBean(User.class));

Inside User.class does the property employeNo need to be in capital letter?

User.class里面的属性employe不需要大写吗?

private String EMPLOYEENO; 
//get/set for EMPLOYEENO


If I change the EMPLOYEENOto small letter, it doesn't work. Can anyone explain why the variable name must be all capital letters?


如果我将 更改EMPLOYEENO小写字母,则不起作用。谁能解释为什么变量名必须全部大写?

采纳答案by Pascal Thivent

See the Hibernate 3.2: Transformers for HQL and SQLblog post:

请参阅Hibernate 3.2:HQL 和 SQL 转换器博客文章:

SQL Transformers

With native sql returning non-entity beans or Map's is often more useful instead of basic Object[]. With result transformers that is now possible.

List resultWithAliasedBean = s.createSQLQuery(
  "SELECT st.name as studentName, co.description as courseDescription " +
  "FROM Enrolment e " +
  "INNER JOIN Student st on e.studentId=st.studentId " +
  "INNER JOIN Course co on e.courseCode=co.courseCode")
  .addScalar("studentName")
  .addScalar("courseDescription")
  .setResultTransformer( Transformers.aliasToBean(StudentDTO.class))
  .list();

StudentDTO dto =(StudentDTO) resultWithAliasedBean.get(0);

Tip: the addScalar()calls were required on HSQLDB to make it match a property name since it returns column names in all uppercase (e.g. "STUDENTNAME"). This could also be solved with a custom transformer that search the property names instead of using exact match - maybe we should provide a fuzzyAliasToBean()method ;)

SQL 转换器

使用本机 sql 返回非实体 bean 或 Map 通常比 basic 更有用Object[]。现在可以使用结果转换器。

List resultWithAliasedBean = s.createSQLQuery(
  "SELECT st.name as studentName, co.description as courseDescription " +
  "FROM Enrolment e " +
  "INNER JOIN Student st on e.studentId=st.studentId " +
  "INNER JOIN Course co on e.courseCode=co.courseCode")
  .addScalar("studentName")
  .addScalar("courseDescription")
  .setResultTransformer( Transformers.aliasToBean(StudentDTO.class))
  .list();

StudentDTO dto =(StudentDTO) resultWithAliasedBean.get(0);

提示:addScalar()在 HSQLDB 上需要调用以使其匹配属性名称,因为它以全部大写形式返回列名称(例如“STUDENTNAME”)。这也可以通过搜索属性名称而不是使用精确匹配的自定义转换器来解决 - 也许我们应该提供一种fuzzyAliasToBean()方法;)

Maybe you are facing the same situation than the one described in the tip in which case you should try to add calls to addScalar().

也许您面临的情况与提示中描述的情况相同,在这种情况下,您应该尝试将调用添加到addScalar().

回答by Misha

Consider putting column aliases in double quotes. Oracle doesn't uppercase aliases specified in double quotes.

考虑将列别名放在双引号中。Oracle 不使用双引号指定的大写别名。

Query query =  getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(
                 "select proj_employee.employee_no \"employeeNo\", ...
 .setResultTransformer(Transformers.aliasToBean(User.class))

;

;