Hibernate的Transformers.aliasToBean()方法 [英] Hibernate's Transformers.aliasToBean() method
问题描述
Query query = getHibernateTemplate()。getSessionFactory()。getCurrentSession()。createSQLQuery(
select proj_employee.employee_no as employeeNo,...
.setResultTransformer (Transformers.aliasToBean(User.class));
里面的User.class
是propertyNumbersNo需要大写字母?
private String EMPLOYEENO;
// get / set for EMPLOYEENO
如果我更改 EMPLOYEENO
to 小写字母,它不起作用。任何人都可以解释为什么变量名必须全部是大写字母?
请参阅 Hibernate 3.2:用于HQL和SQL的变换器博客文章: b
$ b
SQL变形金刚
使用本地sql返回非实体
beans o r Map通常更有用
,而不是基本的Object []
。
结果变换器现在可以
。
$ b $ pre $列表resultWithAliasedBean = s.createSQLQuery(
SELECT st.name as studentName,co.description as courseDescription+
FROM Enrollment 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()
方法
;)
也许你面对的情况与在这种情况下,您应该尝试添加对 addScalar()
的调用。
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?
private String EMPLOYEENO;
//get/set for EMPLOYEENO
If I change the EMPLOYEENO
to small letter, it doesn't work. Can anyone explain why the variable name must be all capital letters?
See the Hibernate 3.2: Transformers for HQL and SQL blog post:
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 afuzzyAliasToBean()
method ;)
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()
.
这篇关于Hibernate的Transformers.aliasToBean()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!