Hibernate AliasToBean Transformer
admin originally posted this on Eldorado Software.
Problem: You need to access values from tables that are not hibernate entities, in an aplication that uses hibernate to access the database.Problem: You want to access a couple columns from any number of tables in your database without bringing back all the associated objects.
Solution: AliasToBean Transformer allows you to retrieve specific information in non entity beans.
This hibernate API call will allow you to run sql against your database and populate a list of pojos that are not a hibernate entity. This technique is great when you need specific information or perhaps you want information fom multiple tables.
Your pojo:
Class PlainOldObject {
String s1;
String s2;
public String getS1() {return s1;}
public String getS2() {return s2;}
public void setS1(String s1) {this.s1=s1;}
public void setS2(String s2) {this.s2=s2;}
}
Then in a hibernate dao :
publc List
// the sql with a reference to the s1 and s2 fields in our object
String sql="select tableX.col1 as s1, tableY.colx as s2 where blah blah blah";
List
.addScalar("s1")
.addScalar("s2")
.setResultTransformer(Transformers.aliasToBean(PlainOldObject.class ) )
.setCachMode(CacheMode.GET)
.list();
return list;
}
This avoids your needing to cycle over multiple objects to pull in the information you need. It also alleviates the need to cycle over Object[] s to sort through your results.
A couple notes:
1. Use the set scalar method to convert the variables in your sql to the fields in your pojo.
2. Use CacheMode.GET to keep these ojects out of the hibernate session cache.