Results 1 to 6 of 6

Thread: How to use hibernate criteria API smartly

  1. #1
    Join Date
    Jul 2005
    Posts
    13

    Default How to use hibernate criteria API smartly

    Hi all, I am new to Spring + Hibernate and thinking about the architecture problem when using the Hibernate Criteria.

    In my existing small project, I am using 3-layers design (web tier, services and persistance layer). Here, I have a Person model class like this

    Code:
    public class Person {
        String name;
        Date birthDate;
    
        ...
    }
    In the web-layer I would like to let the user to search a list of Person that born before/after a user defined date and with name equals, for example, John. I would like to use the Criteria API in Hibernate, but where should I construct the Criteria ? It seems reasonable to perform it in DAO, however, how can I pass these searching criteria to DAO. If it is built in the services class, it violate the rules that services layer should not aware of persistance layer's implementation.

    Could someone can help ? Thanks in advance :roll:

  2. #2
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default Re: How to use hibernate criteria API smartly

    Quote Originally Posted by gaplo
    but where should I construct the Criteria ? It seems reasonable to perform it in DAO, however, how can I pass these searching criteria to DAO.
    The criteria should remain in the dao. You have to add a method to your person dao:
    findByNameAndDateRange(String name, Date fromDate, Date toDate);

    This method will use a criteria... but from the outside nobody needs to know how it is done...

  3. #3
    Join Date
    Jul 2005
    Posts
    13

    Default

    Thanks for your reply.

    Is that mean, I have to write a method for each combination ? for example I added a field nationality to the Person class that I need the followings ?

    Code:
    findByNationalityAndDateRange();
    findByNameAndNationality();
    If more fields are added, the DAO will full with this kind of findByXXX method ?

  4. #4
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    Quote Originally Posted by gaplo
    Thanks for your reply.

    Is that mean, I have to write a method for each combination ? for example I added a field nationality to the Person class that I need the followings ?

    Code:
    findByNationalityAndDateRange();
    findByNameAndNationality();
    If more fields are added, the DAO will full with this kind of findByXXX method ?
    Yes... but if you want to combine a lot of methods, maybe you could better write a single find method with a lot of arguments. If arguments are null, then they are not used.

    eg:
    findBy(String name, Date birthDate, Nationality nationality, Color hairColor, etc etc etc).

    I haven`t had much need so far to create loads an loads of find methods... maybe 2/10 finders per dao.

  5. #5
    Join Date
    Aug 2004
    Posts
    1,905

  6. #6
    Join Date
    Jul 2005
    Posts
    13

    Default

    Thanks all of you :P

Similar Threads

  1. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  2. using hibernate criteria and filters
    By spring_hib in forum Data
    Replies: 3
    Last Post: Jan 14th, 2005, 09:29 AM
  3. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  4. Replies: 3
    Last Post: Nov 5th, 2004, 08:42 AM
  5. Handling criteria without Hibernate dependancies?
    By smccrory in forum Architecture
    Replies: 3
    Last Post: Aug 23rd, 2004, 12:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •