-->
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.  [ 4 posts ] 
Author Message
 Post subject: how to do order-by in hibernate mapping
PostPosted: Wed Sep 02, 2009 2:58 am 
Newbie

Joined: Wed Nov 29, 2006 12:27 am
Posts: 7
suppose Object A has a list of Object B, and Object B must have a object C, B can be order base on C.level .

In A.hbm.xml
Code:
       
<bag name="listB"
             table="T_B"
             inverse="false"
             order-by="?? what should i do here???"
         >
            <key column="ID_A" not-null="true"/>
            <many-to-many column="ID_B" class="B"/>
        </bag>


Top
 Profile  
 
 Post subject: Re: how to do order-by in hibernate mapping
PostPosted: Wed Sep 02, 2009 3:38 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 5:28 am
Posts: 42
shrimpy wrote:
suppose Object A has a list of Object B, and Object B must have a object C, B can be order base on C.level .

In A.hbm.xml
Code:
       
<bag name="listB"
             table="T_B"
             inverse="false"
             order-by="?? what should i do here???"
         >
            <key column="ID_A" not-null="true"/>
            <many-to-many column="ID_B" class="B"/>
        </bag>


<bag name="listB"
table="T_B"
inverse="false"

order-by="ID_B"
>
<key column="ID_A" not-null="true"/>
<many-to-many column="ID_B" class="B"/>
</bag>

_________________
Warm Regards,
Tarun Walia


Top
 Profile  
 
 Post subject: Re: how to do order-by in hibernate mapping
PostPosted: Wed Sep 02, 2009 9:02 pm 
Newbie

Joined: Wed Nov 29, 2006 12:27 am
Posts: 7
in the order-by, can we put SQL query? or HQL query???


tarun walia wrote:
shrimpy wrote:
suppose Object A has a list of Object B, and Object B must have a object C, B can be order base on C.level .

In A.hbm.xml
Code:
       
<bag name="listB"
             table="T_B"
             inverse="false"
             order-by="?? what should i do here???"
         >
            <key column="ID_A" not-null="true"/>
            <many-to-many column="ID_B" class="B"/>
        </bag>


<bag name="listB"
table="T_B"
inverse="false"

order-by="ID_B"
>
<key column="ID_A" not-null="true"/>
<many-to-many column="ID_B" class="B"/>
</bag>


Top
 Profile  
 
 Post subject: Re: how to do order-by in hibernate mapping
PostPosted: Thu Sep 03, 2009 2:14 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 5:28 am
Posts: 42
use Criteria API for ordering
Code:
Client user = new Client();
Session session = HibernateUtil.beginTransaction();
Criteria criteria = session.createCriteria(Client.class);
Order order = Order.asc("clientID");
criteria.addOrder(order);
List results = criteria.list();
HibernateUtil.commitTransaction();
for (int i = 0; i<results.size(); i++) {
System.out.println(results.get(i).toString());
}
}


or a simple orderby in the HQL:

Code:
Session session = HibernateUtil.beginTransaction();
String hql = "from Client as cln ORDER BY u.clientId ASC";
Query query = session.createQuery(hql);
List client= query.list();
for (int i = 0; i < client.size(); i++) {
Client cln = (Client) client.get(i);
System.out.println(cln.getName());
}
HibernateUtil.commitTransaction();

_________________
Warm Regards,
Tarun Walia


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.