| I added a solution for this issue on GitHub. Basically, we need to disable the collection fetching mechanism and override its mechanism like in the following result set transformer example:
Node root = (Node) doInHibernate(session -> {
return session
.createSQLQuery(
"SELECT * " +
"FROM Node " +
"CONNECT BY PRIOR id = parent_id " +
"START WITH parent_id IS NULL ")
.addEntity(Node.class)
.setResultTransformer(new ResultTransformer() {
@Override
public Object transformTuple(Object[] tuple, String[] aliases) {
Node node = (Node) tuple[0];
if(node.parent != null) {
node.parent.addChild(node);
}
return node;
}
@Override
public List transformList(List collection) {
return Collections.singletonList(collection.get(0));
}
})
.uniqueResult();
});
This requires some modifications in the hibernate-core because this cannot work if the children association is a @OneToMany association, as it will trigger an additional query upon being traversed. |