Joining tables using criteria and sqlProjection
I have the following function that builds a Hibernate Criteria to generate binned data:
private Criteria getCustomBinResultCriteriaSQL(double binSizeX, double binSizeY, String xVar, String yVar, String customBinQuery) {
return createCriteria(Residue.class)
.setProjection(Projections.projectionList()
.add(Projections.sqlGroupProjection(
"floor(" + xVar + " / " + binSizeX + ") * " + binSizeX + " as xBin, " +
"floor(" + yVar + " / " + binSizeY + ") * " + binSizeY + " as yBin, " +
"CAST (" + customBinQuery + " AS DOUBLE PRECISION) as customBinResult",
"xBin, yBin",
new String[] { "xBin", "yBin", "customBinResult" },
开发者_如何学运维 new Type[] { Hibernate.DOUBLE, Hibernate.DOUBLE, Hibernate.DOUBLE })))
.setResultTransformer(Transformers.aliasToBean(CustomBinResult.class));
}
This all works pretty well for data within the same table (residue), but let's say my datastructure is like this:
pdbentry: id pdbvar expmethod: id expvar residue: id resvar
pdbentry has a one-to-one relation with expmethod, and a one-to-many relation with residue.
How would I go about joining the residue table with expmethod, based on the criteria-builder above. So in other words: what do I need to add to the criteria to be able to have "expvar" as xVar?
I tried adding something like:
.setFetchMode("pdbentry", FetchMode.JOIN);
.setFetchMode("expmethod", FetchMode.JOIN);
at the end, but then I still couldn't put "expvar" nor "expmethod.expvar" as xVar.
Any ideas?
I'm aliasing the joined-to tables via
.createCriteria("rootEntity.foreignEntity", "someAlias")
This makes rootEntity.foreignEntity.someProperty
available to the Criteria query as someAlias.someProperty
(usually I choose "someAlias" to be the same as "foreignEntity").
Since I usually want an eager fetch, I tend to have Criteria.INNER_JOIN
or Criteria.LEFT_JOIN
on the createCriteria call, too.
Have you tried "pdbentry.expmethod.expvar"?
It is not clear (to me) what you are trying to do from just this, but the way you define your entity relationships, the way to access the relevant expvar from a given residue would be "residue.pdbentry.expmethod.expvar".
精彩评论