Hibernate Criteria Extension (HCE) is a extension library for hibernate, HCE is not a substitution of hibernate.
HCE Architecture :
HCE can dynamically detect an association join through hibernate annotation configuration.
- Hibernate Criteria
Actually,
when we select animal, it's country will be fetched automatically as
the fetch type is EAGER. However, when we have a large of object
and the projections declaration is needed for keeping
performance. We must override the fetch type by using criteria,
hql, or sql query. We will use criteria for the example.
Code
:
DetachedCriteria
detachedCriteria = DetachedCriteria.forClass(Animal.class,
"animal");
detachedCriteria.createAlias("animal.country",
"country");
detachedCriteria.setProjection(Projections.projectionList()
.add(Projections.property("animal.id"))
.add(Projections.property("animal.name"))
.add(Projections.property("country.name"))
);
Unfortunately,
the code above throw an exception
: java.lang.ClassCastException:
[Ljava.lang.Object; cannot be cast to Animal.
Hibernate
cannot return an association selected field when using projections.
In this case, we select “country.name” and hibernate cannot
set the value of name to country. We need to declare new
property and give alias for “country.name”. This will
happened to every associated projections.
- HCE Query
Code
:
DetachedCriteria
detachedCriteria = criteriaWithProjections(new
String[] {"id",
"name",
"country.name"});
The
sql query will be looked like :
select
this_.id as
y0_,
this_.name as
y1_,
country0x1_.name
as y2_
from
Animal this_
inner join
country
country0x1_
on
this_.countryId=country0x1_.id
The
example result will be looked like :
Animal@40e99ce5[id=1,name=Panda,numberOfFeet=0,food=<null>,description=<null>,country=softtech.hong.hce.junit.model.Country@9706da8[id=<null>,code=<null>,name=China,description=<null>,shortname=<null>,active=false,cities=<null>]]
Hibernate
Criteria Extension (HCE) is written to solve hibernate limitation.
HCE can simplify your code. HCE API can let you to select part of
columns that you need, we just give the field names that we want to
select. HCE support for association and automatically detect join
type from entity mapping. HCE is written based on hibernate criteria
and projections.
HCE
offer you a better way to generate query script without spend too
much time. HCE give you a simple way to declare select, restriction,
and order.
HCE’s
goal is to relieve developer a better way to select columns without
ignore the performance issue. HCE may not be the best solution for
every conditions. However, HCE can certainly help you to simplify
projections declaration, restrictions with association, and order.
Why
Hibernate Criteria Extension (HCE) ?
HCE
give us some reasons on why it should be used :
- I suggest that we should get the data only that we need. Although for this time there is not performance issue when selecting a table, we have never know for future if it still make sense for us to get all the column data at one time that we don't need all of them absolutely (not every field is used). In Hibernate, we can use projections. Using projections need more code to do.
- Let hibernate select all columns in a entity causing debug problem. We will be confused if we only want to track one column value. We need to substract the query generated before copy and paste to database browser.
- Using criteria and projections prevent n+1 select problem. By using HCE and give the projections, we will override the default configuration of hibernate, the columns to be selected is chosen well.
- The less code you write, the less time you need to build an application. By using HCE, I hope we reduce to write method. We can use one method for many solutions.
HCE Repository :
hce-hibernate3x => https://github.com/kismanhong/hce
hce-hibernate4x => https://github.com/kismanhong/hce-hibernate4xHCE documentation can be seen from http://plihplih.com/hce/hce.pdf and You can see the API at http://plihplih.com/hce
No comments:
Post a Comment