Hibernate 3.2: transformers for hql and SQL

Source: Internet
Author: User

Http://bbs.xml.org.cn/blog/more.asp? Name = lhwork & id = 15351

 

People using the criteria Api have either transparently or knowingly used
Resulttransformer

. A resulttransformer is a nice and
Simple interface that allows you to transform any criteria result element. E. g.
You can make any criteria result be returned as a java. util. map or as
Non-Entity Bean.

Criteria Transformers

Imagine you have a studentdto class:

public class StudentDTO {
private String studentName;
private String courseDescription;

public StudentDTO() { }

...
}

Then you can make the criteria return non-entity classes instead of scalars
Or entities by applying a resulttransformer:

List resultWithAliasedBean = s.createCriteria(Enrolment.class)
.createAlias("student", "st").createAlias("course", "co")
.setProjection( Projections.projectionList()
.add( Projections.property("st.name"), "studentName" )
.add( Projections.property("co.description"), "courseDescription" )
)
.setResultTransformer( Transformers.aliasToBean(StudentDTO.class) )
.list();

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

This is how resulttransformer have been available since we introduced projection

To the criteria API in hibernate 3.

It is just one example of the built in transformers and users can provide
Their own transformers if they so please.

Jealous Programming

Since I am more a hql/SQL guy I have been jealous on criteria for having this
Feature and I have seen limit requests for adding it to all our query facilities.

Today I put an end to this jealousy and introduced resulttransformer for hql
And SQL in hibernate 3.2.

Hql Transformers

In hql we already had a "Kind" of result transformers via the ("select new"
Http://www.hibernate.org/hib_docs/v3/reference/en/html/queryhql.html#queryhql-select

)
Syntax, but for returning non-entity beans it only provided value injection
These beans via its constructor. Thus if you used the same DTO in your different
Scenarios you cocould end up having constructors on this DTO purely
Allowing the "select new" functionality to work.

Now you can get the value injected via property methods or fields instead,
Removing the need for explicit constructors.

List resultWithAliasedBean = s.createQuery(
"select e.student.name as studentName," +
" e.course.description as courseDescription" +
"from Enrolment as e")
.setResultTransformer( Transformers.aliasToBean(StudentDTO.class))
.list();

StudentDTO dto = (StudentDTO) resultWithAliasedBean.get(0);
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 () callwere required on HSQLDB to make it match
Property name since it returns column names in all uppercase (e.g.
"Studentname"). This coshould also be solved with a custom transformer that search
The property names instead of using exact match-maybe we shoshould provide
Fuzzyaliastobean () method

Map vs. object []

Since you can also use a transformer that return a map from alias
Value/entity (e.g. Transformers. alias_to_map), you are no longer required
Mess with index based object arrays when working with a result.

List iter = s.createQuery(
"select e.student.name as studentName," +
" e.course.description as courseDescription" +
"from Enrolment as e")
.setResultTransformer( Transformers.ALIAS_TO_MAP )
.iterate();

String name = (Map)(iter.next()).get("studentName");

Again, this works equally well for criteria, hql and native SQL.

Reaching Nirvana of native SQL

We still miss

A few

Things, but with the addition of resulttranformer
Support for SQL and the other

Additions lately

To the native SQL functionality in hibernate we
Are close to reach the nirvana of native SQL support.

Combined with statelesssession

You actually now got a very flexible
And full powered "SQL executor" which transparently can map to and from objects
With native SQL without any ORM overhead.

... And when you get tired of managing the SQL, objectstate, lifecycles,
Caching etc. Of your objects manually and want to benefit from the power of
Orm then you got it all readily available to you

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.