-->
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.  [ 2 posts ] 
Author Message
 Post subject: Paging (and searching) entities from an association
PostPosted: Sat Aug 15, 2009 11:31 am 
Newbie

Joined: Sat Aug 15, 2009 10:44 am
Posts: 2
Hi all,
I am new to Hibernate, I have a couple probably relatively simple questions :

I have two entities, Department and Employee, representing two db tables, connected in the one-to-many relation (Employee db table, as usual, have foreign key pointing to the Department table) :
Code:
@Entity
@Table(name = "DEPARMENT", uniqueConstraints = {...})
public class Department implements java.io.Serializable {
  private Set<Employee> employees = new HashSet<Employee>(0);
@OneToMany(fetch = FetchType.LAZY, mappedBy = "...")
   public Set<Employee>  getEmployees() {
      return this.employees;
   }

   public void setEmployees()(Set<Employees> employees) {
      this.employees = employees;
   }
}

@Entity
@Table(name = "EMPLOYEE", uniqueConstraints = {...})
public class Employee implements java.io.Serializable {
   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "DEPARTMENT_ID", nullable = false)
   public TeeDrzava getDepartment() {
      return this.department;
   }
}

- So, nothing special. As you can see, for a particular Department instance, I can get all employees in that department by calling department.getEmployees(). Now, what I want, is :
1. Determine total number of employees for that department. Can I simply use
Code:
       department.getEmployees().size()
   

/and what hibernate will do in this case - pre-load all employees into memory ?/ or exist a better (cheaper) way to do this ?

2. I want to 'paging' throughout employees obtained with department.getEmployees(); in the same way as creating Query an setting first result /setFirstResult();/ and setMaxResults(). So, starting from the n-th object, I want to retrieve the next m objects. How to do that ?

3. I want to further refine search throughout department.getEmployees() - let user to propose search criteria, and to find all Employees (for particular department) conforming that search criteria. How to do this ? Should I make Query ("from Employee e where e.department = ? and e.name like ?" ) or I can achieve this somehow with Java code and maybe annotation ?
- thanks in advance,...


Top
 Profile  
 
 Post subject: Re: Paging (and searching) entities from an association
PostPosted: Sun Aug 16, 2009 6:14 am 
Newbie

Joined: Sat Aug 15, 2009 10:44 am
Posts: 2
-what commes in my mind, is a (probably stupid) idea to write another method in a master entity Department class, which accepts parameters for the firstResult and maxResult, and retrieves paginated employees, something like that :
Code:
  @Entity
  public class Department ...{
    public Set<Employee>  getPaginatedEmployees(int firstResult, int maxResult) {
        Query empQ = entityManager.
                                   createQuery("select e from Employee e where  e.department = ?");
        empQ.setMaxResults(maxResult);
    empQ.setFirstResult(firstResult);
    return empQ.getResultList();
    }
  }

-but... do not know if this will work..please some from Hibernate team for help...


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