-->
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.  [ 1 post ] 
Author Message
 Post subject: hibernate criteria issue(selecting from two tables)
PostPosted: Mon Jul 09, 2012 10:05 am 
Newbie

Joined: Mon Jul 09, 2012 9:59 am
Posts: 1
i have next tables:

Code:
  Table table1
    table1Id
    field1
    field2
    field3
   
    Table table2
    table2Id
    table2_field1
    table2_field2
    table2_field3
    table1Id

in this method i get Objects from table1 sorted by some field


Code:
    public List<table1> getMost() {
          
          DetachedCriteria criteria = (DetachedCriteria) DetachedCriteria.forClass(table1.class);
          //criteria.add(Restrictions.conjunction());
          criteria.addOrder(Order.desc("field1"));
          
          List<table1> myList = (List<table1>) findByCriteria(criteria,
                    false, 0, 10);//get first 10 elements by some criteria
          return myList;
       }


then i need to get Objects from database sorted by some field, but these Objects depend on Objects from table1

Code:

    public Item getTheBest(Long table1Id) {
   
            DetachedCriteria criteria = (DetachedCriteria) DetachedCriteria
                    .forClass(Item.class);
   
            DetachedCriteria maxQuery = DetachedCriteria.forClass(Item.class);
            maxQuery.add(Restrictions.eq("table1Id", table1Id)).setProjection(
                    Projections.max("table2_field1"));
            criteria.add(Restrictions.and(
                    Restrictions.eq("table1Id", table1Id),
                    Property.forName("table2_field1").eq(maxQuery)));
            List<Item> result = (List<Item>) findByCriteria(criteria, false);
   
            if (result.iterator().hasNext())
                return result.iterator().next();
            else
                return null;
   
        }
   



what i want to have is method like this:

Code:
    public Item getTheBest(List<Long> table1Ids)


thus this method composes these two above methods and makes less calculations.
the idea of the method is to have a Collection of Objects, sorted by one criteria and after sorting by this criteria, we choose items by some field.
the
so how can i do it in hibernate?
so
Code:
public Item getTheBest(List<Long> table1Ids)
method must select data based on ids from the first table and from the selected elements would be chosen elements, chosen via sorting.


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

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.