-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: how to flatten/unfold the Criteria result (object hierarchy)
PostPosted: Thu Oct 19, 2006 11:16 am 
Newbie

Joined: Mon Jul 24, 2006 1:35 pm
Posts: 10
Hello,

Is there a way to tell Criteria object to return data as a flattened view (all objects/tables returned as a single row) instead of an object hierarchy? Something like this:

Parent p1, Child c1
Parent p1, Child c2
Parent p1, Child c3
Parent p2, Child c1

Instead of:

Parent p1 (Child c1, Child c2, Child c3)
Parent p2 (Child c1)

Most of the time I'm using the hierarchy, which works better for business operations. But this time I need to write a report, and a flat view would be better. So - can this be done with Criteria?
Thanks,

Bratek


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 19, 2006 12:08 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I think you can do it with projections. e.g.

Code:
List results = session.createCriteria(Parent.class, "parent") // Criteria on the parent class
    .createAlias("children", "child")  // Alias the children collection
    .setProjection( Projections.projectionList() // Create projections for each column in result
        .add( Projections.property("parent.property1"), "parentProperty1" )
        .add( Projections.property("parent.property2"), "parentProperty2" )
        .add( Projections.property("child.property1"), "childProperty1" )
        .add( Projections.property("child.property2"), "childProperty2" )
    )
    .addOrder( Order.asc("parentProperty1") ) // When you alias the projections, you can
    .addOrder( Order.asc("childProperty2") ) //  refer to them later
    .list();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 20, 2006 11:32 am 
Newbie

Joined: Wed Aug 03, 2005 6:49 pm
Posts: 9
Havent tried it but another way could be

session.createCriteria(Parent.class)
.createAlias("children", "child")
.returnMaps()
.list();

Each list entry is a map with containing the parent and the child entries (each child in a separate entry but with the same parent)



Ananasi wrote:
I think you can do it with projections. e.g.

Code:
List results = session.createCriteria(Parent.class, "parent") // Criteria on the parent class
    .createAlias("children", "child")  // Alias the children collection
    .setProjection( Projections.projectionList() // Create projections for each column in result
        .add( Projections.property("parent.property1"), "parentProperty1" )
        .add( Projections.property("parent.property2"), "parentProperty2" )
        .add( Projections.property("child.property1"), "childProperty1" )
        .add( Projections.property("child.property2"), "childProperty2" )
    )
    .addOrder( Order.asc("parentProperty1") ) // When you alias the projections, you can
    .addOrder( Order.asc("childProperty2") ) //  refer to them later
    .list();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.